【发布时间】:2016-01-27 14:32:22
【问题描述】:
我有一个这样的数据框
Datetime <- c("2015-12-31 08:30:13", "2015-12-31 12:45:00", "2016-01-01 02:53:20", "2016-01-01 03:22:18",
"2016-01-01 09:42:10", "2016-01-01 20:55:50", "2016-01-01 21:14:10", "2016-01-02 05:42:16",
"2016-01-02 08:31:15", "2016-01-02 09:13:10", "2016-01-03 00:45:14", "2016-01-03 05:56:00",
"2016-01-03 13:44:00", "2016-01-03 14:41:20", "2016-01-03 15:33:10", "2016-01-04 04:24:00",
"2016-01-04 17:24:12", "2016-01-04 17:28:16", "2016-01-04 18:22:34", "2016-01-05 02:34:31")
Measurement <- c("Length","Breadth","Height","Length",
"Breadth","Breadth","Breadth","Length",
"Length","Breadth","Height","Height",
"Height","Length","Height","Length",
"Length","Breadth","Breadth","Breadth")
df1 <- data.frame(Datetime,Measurement)
我正在尝试以这种格式对日期进行子集化
Day1 = December 31st,2015 at 6:30AM to January 1st 2016 6:30AM
Day2 = January 1st,2015 at 6:30AM to January 2nd 2016 6:30AM
etc..
在执行此操作的同时,我还想将 Measurement 列转换为具有每个类别计数的单独列
我想要的输出是
Days Length Breadth Height
Day1 2 1 1
Day2 1 3 0
Day3 1 1 2
Day4 2 0 2
Day5 1 3 0
我尝试了这样的方法来获取日期范围
today <- as.POSIXlt(Sys.time())
today$mday <- today$mday + (today$wday-(today$wday+27))
today$hour = "6";today$min = "30";today$sec = "0"
Back1Day <- today
Back1Day$mday <- today$mday-1
我如何根据这个问题进行子集化。我尝试使用 dcast 来做,但没有做对。
df2 <- dcast(df1, Datetime ~ Measurement)
请就此提供一些指导。
【问题讨论】:
-
"2015-12-31 02:53:20"属于哪一天还不是很清楚。 -
大卫,感谢您指出。对不起,我犯了一个错误,我刚刚编辑了它“2016-01-01 02:53:20”
-
第一天总是在早上 6:30 之后开始吗?否则它应该属于您的日期范围的前一天
-
是的。例如:为了计算 Jan1 的测量次数,我查看了这个日期范围(12 月 31 日 6:30 到 Jan1 6:30)。这就是我在更大数据集中使用的逻辑
标签: r datetime subset reshape2