简体   繁体   English

如何在R中对日期时间进行子集化并转动测量列

[英]How do I subset datetimes and pivot the measurement column in R

I have a dataframe like this 我有这样的数据帧

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)

I am trying to subset the dates in this format 我试图以这种格式对日期进行子集化

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..

While doing this, I would also like to pivot the Measurement column into its individual columns with count of each category 在执行此操作时,我还希望将“测量”列转换为各个列,并列出每个类别的计数

My desired output is 我想要的输出是

Days Length Breadth Height
Day1      2       1      1
Day2      1       3      0
Day3      1       1      2
Day4      2       0      2
Day5      1       3      0

I tried something like this to get the date ranges 我试过这样的东西来获得日期范围

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

How do I subset according to this problem. 如何根据此问题进行子集化。 I tried to do it using dcast but not getting it right. 我尝试使用dcast但没有做到正确。

df2 <- dcast(df1, Datetime ~ Measurement)

Kindly provide some directions on this. 请提供一些指示。

This seem to satisfy your needs (according to your comments). 这似乎满足您的需求(根据您的意见)。 I'm just creating a sequence from the first date to the last one by day, and then utilizing the findInterval function in order to match the days. 我只是创建一个从第一个日期到最后一个日期的序列,然后利用findInterval函数来匹配日期。 Then, a simple dcast gives you what you need. 然后,一个简单的dcast为您提供所需。

library(data.table)
setDT(df1)[, Datetime := as.POSIXct(Datetime)] ## First need to convert to POSIXct class
df1[, Days := paste0("Day", findInterval(Datetime, 
                              seq(as.POSIXct(paste(as.Date(Datetime[1L]), "6:30")), 
                                  as.POSIXct(paste(as.Date(Datetime[.N]), "6:30")), 
                             by = "day")))]
dcast(df1, Days ~ Measurement)
#    Days Breadth Height Length
# 1: Day1       1      1      2
# 2: Day2       3      0      1
# 3: Day3       1      2      1
# 4: Day4       0      2      2
# 5: Day5       3      0      1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM