Summarize dataframe by day from timestamp

不打扰是莪最后的温柔 提交于 2019-11-29 17:05:18

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!