使用 R 中的摘要数据创建新列

Creating a new column using summary data in R

数据:

structure(list(datetime = structure(c(6L, 2L, 4L, 5L, 1L, 3L), .Names = c("V1", 
"V2", "V3", "V4", "V5", "V6"), .Label = c(" 2016-12-01 00:00:30", 
" 2016-12-01 00:02:17", " 2016-12-01 00:06:17", " 2016-12-01 00:28:10", 
" 2016-12-01 01:17:02", "2016-12-01 00:00:00"), class = "factor")), .Names = "datetime", row.names = c("V1", 
"V2", "V3", "V4", "V5", "V6"), class = "data.frame")

代码

library(lubridate)
library(dplyr)

data$datetime <- ymd_hms(data$datetime)
data <- dplyr::arrange(data, datetime)
data$hour <- cut.POSIXt(data$datetime, "hour")
data %>% group_by(hour) %>% summarize(count = n())

输出 小标题:2 x 2 小时数 1 2016-12-01 00:00:00 5 2 2016-12-01 01:00:00 1

原始数据集中的输出 日期时间小时 2016-12-01 00:00:00 00 2016-12-01 00:00:01 00

期望的输出

    DateTime     Hour   Count
               <fctr> <int>
1 2016-12-01   00:00:00     5
2 2016-12-01   01:00:00     1

我想显示每小时的记录数,并将这些数字放入名为计数的新列中。希望你们理解我的问题。 请大家帮帮我..

选项是将 separate 添加到 %>%

library(tidyr)
res <- data %>%
         group_by(hour) %>%
         summarize(count = n()) %>%
         separate(hour, into = c('DateTime', 'Hour'), sep=' ')

可以把group_by/summarize改成count

res <- count(data, hour) %>%
          separate(hour, into = c('DateTime', 'Hour'), sep=' ')