I have a dataset data
that contains a timestamp and a suite of other variables with values at each timestamp. I am trying to use ddply
within
You can use format
to extract the day
dat$DAY <- as.factor(format(dat$TIMESTAMP,'%d'))
[1] 01 01 01 01 01 02 02 02 02 02 07 07 07 07 07
Levels: 01 02 07
For some reasons I get error if I don't remove the first column ( as said in comment in moment I am writing this)
ddply(data[,-1],.(DAY),summarise, V1 = mean(P), V2 = max(WS))
DAY V1 V2
1 01 992.4 6.259
2 02 992.4 4.023
3 07 239.0 3.129
converting the TIMESTAMP to POSIXct
seems to correct the problem
dat$TIMESTAMP <- as.POSIXct(dat$TIMESTAMP)
ddply(dat,.(DAY),summarise, V1 = mean(P), V2 = max(WS))
DAY V1 V2
1 01 992.4 6.259
2 02 992.4 4.023
3 07 239.0 3.129
EDIT no need to use format
Since Your TIMESTAMP column is a POSIXlt
, it easy to retrieve day part from it, You can do this :
dat$TIMESTAMP$mday
[1] 1 1 1 1 1 2 2 2 2 2 7 7 7 7 7