[R] separate date and time

KEn katakagi at bu.edu
Thu May 12 17:33:13 CEST 2011


Schatzi <adele_thompson <at> cargill.com> writes:

> 
> I have a combined date and time. I would like to separate them out into two
> columns so I can do things such as take the mean by time across all dates.
> 
> meas<-runif(435)
> nTime<-seq(1303975800, 1304757000, 1800)
> nDateT<-as.POSIXct(nTime, origin="1970-01-01")
> mat1<-cbind(nDateT,meas)
> 
> means1<- aggregate(mat1$meas, list(nDateT), mean)
> 
> This doesn't do anything as each day is different, but if I had just the
> time, it would take the mean outputing 48 values (for each 30 min).
> 
> Also, sometimes there are missing meas to a specific time. Is there anyway
> to copy the previous meas if one is missing?
> 
> -----
> In theory, practice and theory are the same. In practice, they are not -
Albert Einstein
> --
> View this message in context:
http://r.789695.n4.nabble.com/separate-date-and-time-tp3517571p3517571.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> 

Not sure if this is what you want, but you can use substr to split nDateT into
date and time, and then use aggregate() in the time column in df1.

meas<-runif(435)
nTime<-seq(1303975800, 1304757000, 1800)
nDateT<-as.POSIXct(nTime, origin="1970-01-01")
date <- substr(nDateT, 1, 10)
time <- substr(nDateT, 12, 19)
df1 <- data.frame(date, time, meas)

means1<- aggregate(df1$meas, list(df1$time), mean)

HTH,
Ken



More information about the R-help mailing list