【问题标题】:Creating a new column using summary data in R在 R 中使用汇总数据创建新列
【发布时间】:2017-12-31 02:39:16
【问题描述】:

数据:

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

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

【问题讨论】:

    标签: r time-series data-mining dataset


    【解决方案1】:

    选项是将separate 添加到%&gt;%

    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=' ')
    

    【讨论】:

    • 这仅在 R 控制台中显示摘要。它不会在我的数据集中创建具有计数值的新列。该怎么做??
    猜你喜欢
    • 1970-01-01
    • 2018-03-24
    • 2021-09-01
    • 2021-11-24
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多