[R] evaluate the daily mean with date in "POSIXct" "POSIXt" class

Jim Lemon drjimlemon at gmail.com
Tue Jun 7 09:39:21 CEST 2016


Hi Stefano,
I might be missing something, but try this:

MteBove<-read.table(text="posix_date  posix_time  snow    temp
 2010-01-19 23:30:00 45 NA
 2010-01-20 00:30:00 10 2.7
 2010-01-20 03:00:00 45 NA
 2010-01-20 03:30:00 44 NA
 2010-01-20 04:00:00 44 NA
 2010-01-20 04:30:00 44 NA
 2010-01-20 05:00:00 44 NA
 2010-01-20 05:30:00 45 NA
 2010-01-20 06:00:00 44 NA
 2010-01-20 06:30:00 45 NA
 2010-01-20 23:30:00 45 NA
 2010-01-21 00:30:00 10 2.7
 2010-01-21 03:00:00 45 NA
 2010-01-21 03:30:00 44 NA
 2010-01-21 04:00:00 44 NA
 2010-01-21 04:30:00 44 NA
 2010-01-21 05:00:00 44 NA
 2010-01-21 05:30:00 45 NA
 2010-01-21 06:00:00 44 NA
 2010-01-21 06:30:00 45 NA",
 header=TRUE)
MteBove$day<-as.Date(MteBove$posix_date,"%Y-%m-%d")
by(MteBove$snow,MteBove$day,mean)

and if you have NA values, add na.rm=TRUE to the last line.

Jim


On Tue, Jun 7, 2016 at 4:55 PM, Stefano Sofia
<stefano.sofia at regione.marche.it> wrote:
> Dear R-list users,
> I have the following data frame, called MteBove:
>
> posix_date    snow    temp
> ...
> 2010-01-20 23:30:00 45 NA
> 2010-01-02 00:30:00 10-2.7
> 2010-01-20 03:00:00 45 NA
> 2010-01-20 03:30:00 44 NA
> 2010-01-20 04:00:00 44 NA
> 2010-01-20 04:30:00 44 NA
> 2010-01-20 05:00:00 44 NA
> 2010-01-20 05:30:00 45 NA
> 2010-01-20 06:00:00 44 NA
> 2010-01-20 06:30:00 45 NA
> ...
>
>> sapply(MteBove, class)
> gives
> $posix_date
> [1] "POSIXct" "POSIXt"
>
> $snow
> [1] "numeric"
>
> $temp
> [1] "numeric"
>
> There are semi-hourly data (i.e. 48 data each day) from the first of November to the first of May of each Winter season, and I need the daily mean for snow and temp.
> First I created a subset for each Winter season
> I tried to create a new column with only the day, like
>
>> MteBove$day <- trunc(MteBove$posix_date, "days")
>
> and then
>
>> list_days <- unique(MteBove$day)
>> means <- rapply(list_days, function(x) mean(MteBove[MteBove$day == x, ], na.rm=T))
>
> with the following result:
> [1] "means IS"
>   sec   min  hour  mday   mon  year  wday  yday isdst
>    NA    NA    NA    NA    NA    NA    NA    NA    NA
>
> I tried some changes always in this direction, with no success.
> Is there an efficient way to do that?
> Could somebody give me an hint about it?
>
> Thank you for your attention and your help
> Stefano Sofia
>
>
> ________________________________
>
> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere informazioni confidenziali, pertanto è destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si è il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed urgenza, la risposta al presente messaggio di posta elettronica può essere visionata da persone estranee al destinatario.
> IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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