Hi,
Since you mentioned filtering the daily maximum temperature, this may help:
datos<-read.table(text="
X, Ta, HR, RS, v
1/1/2010 1:00:00,5.28,100,0,2.3
1/1/2010 6:00:00,5.45,100,0,2.5
1/1/2010 11:00:00,5.51,100,0,1.1
2/1/2010 1:00:00,5.33,100,0,2.1
2/1/2010 6:00:00,5.48,100,0,2.3
2/1/2010 11:00:00,5.64,100,0,2.4
3/1/2010 1:00:00,5.12,100,0,2.0
3/1/2010 6:00:00,5.40,100,0,2.3
3/1/2010 11:00:00,5.32,100,0,1.7
",sep=",",header=TRUE,stringsAsFactors=FALSE)
library(zoo)
z<-zoo(datos[,-1],order.by=as.POSIXct(datos[,1],format="%d/%m/%Y %H:%M:%S"))
? aggregate(z$Ta,by=list(as.Date(index(z))),max) #gives the daily maximum temperatures
#2010-01-01 2010-01-02 2010-01-03
? #? ? 5.51? ? ? 5.64? ? ? 5.40
For filtering the data:
flag<-unlist(lapply(split(z,as.Date(index(z))),function(x) data.frame(x[,1]==max(x[,1]))),use.names=F)
? z1<-transform(z,flag=flag)
z2<-z1[,1:4][z1$flag==1,] #filtered the maximum daily temp data
z2
#? ? ? ? ? ? ? ? ? ? ? Ta? HR RS? v
#2010-01-01 11:00:00 5.51 100? 0 1.1
#2010-01-02 11:00:00 5.64 100? 0 2.4
#2010-01-03 06:00:00 5.40 100? 0 2.3
? str(z2)
#?zoo? series from 2010-01-01 11:00:00 to 2010-01-03 06:00:00
? # Data: num [1:3, 1:4] 5.51 5.64 5.4 100 100 100 0 0 0 1.1 ...
? #- attr(*, "dimnames")=List of 2
? #..$ : NULL
? #..$ : chr [1:4] "Ta" "HR" "RS" "v"
? #Index:? POSIXct[1:3], format: "2010-01-01 11:00:00" "2010-01-02 11:00:00" ...
#or
z3<-z
? z3$Max<-ave(z[,1],as.Date(index(z)),FUN=max)
? z4<-z3[,-5][z3$Ta==z3$Max,]
? z4
#? ? ? ? ? ? ? ? ? ? ? Ta? HR RS? v
#2010-01-01 11:00:00 5.51 100? 0 1.1
#2010-01-02 11:00:00 5.64 100? 0 2.4
#2010-01-03 06:00:00 5.40 100? 0 2.3
Hope it helps
A.K.
----- Original Message -----
From: Dominic Roye <dominic.roye at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Friday, November 23, 2012 1:04 PM
Subject: [R] daily maximum temperature
Hello,
I want to filter the daily maximum temperature. For this i made this
skript, but it come out wrong results. Can anybody help me?
Thanks for your help!
Best regards
datos$X <- as.POSIXct(strptime(datos$X, "%d/%m/%Y %H:%M:%S"))
z <- aggregate(zoo(datos$Ta), as.POSIXct(datos$X), max)