[R] daily maximum temperature

arun smartpink111 at yahoo.com
Sat Nov 24 16:52:01 CET 2012


Hi,

If I understand your question, this would do it:
z2<-transform(z2,Hr=format(index(z2),"%H"))
 z2
#                    Ta   HR  RS v   Hr
#2010-01-01 11:00:00 5.51 100 0  1.1 11
#2010-01-02 11:00:00 5.64 100 0  2.4 11
#2010-01-03 06:00:00 5.40 100 0  2.3 06
A.K.



----- Original Message -----
From: Dominic Roye <dominic.roye at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: 
Sent: Saturday, November 24, 2012 6:36 AM
Subject: Re: [R] daily maximum temperature

Hello,

Your first option works well, but how i can get also the hour of the
daily maximum temperature?

Thanks!


2012/11/23 arun <smartpink111 at yahoo.com>:
>
>
> 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)
>
>
>> str(datos)
> 'data.frame':   17137 obs. of  5 variables:
> $ X : Factor w/ 17136 levels "00/01/1900 0:00:00",..: 2 3 4 5 6 7 8 9 10 11 ...
> $ Ta: num  5.28 5.45 5.54 5.54 5.51 5.51 5.5 5.56 5.58 5.63 ...
> $ HR: int  100 100 100 100 100 100 100 100 100 100 ...
> $ RS: int  0 0 0 0 0 0 0 0 0 0 ...
> $ v : num  2.3 2.5 1.1 2.3 2.2 2.1 2.2 2.9 2.4 2.6 ...
>
>
>
> str(z)
> ‘zoo’ series from 2010-01-01 00:10:00 to 2010-04-29 23:50:00
>   Data: num [1:17129] 5.28 5.45 5.54 5.54 5.51 5.51 5.5 5.56 5.58 5.63 ...
>   Index:  POSIXct[1:17129], format: "2010-01-01 00:10:00" "2010-01-01
> 00:20:00" "2010-01-01 00:30:00" "2010-01-01 00:40:00" ...
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>





More information about the R-help mailing list