R:分组表中缺少级别的零填充

时间:2015-12-16 06:35:53

标签: r data.table

我想为具有时间序列事件的数据表创建时间仓的向量。这种向量中的每个元素表示特定时隙内的度量。数据表dt如下:

dt=structure(list(
           hour = c("20", "21", "21", "21", "21", "02", "02", "02", "02", "02"), 
           timeSlt = structure(c(6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L), .Label = c("[0,4)", "[4,8)", "[8,12)", "[12,16)", "[16,20)", "[20,24)"), class = "factor"),
           play_length = c(208.67, 188.49, 58.5, 3.469, 17.92, 211.513, 193.045, 225.306, 212.715, 226.873)), 
           .Names = c("hour", "timeSlt", "length"), 
           class = c("data.table","data.frame"), row.names = c(NA, -10L))

hourtimeSlt分别表示每日小时数和相应的时段。 timeSlt是一个因素:

dt[, timeSlt]
# [1] [20,24) [20,24) [20,24) [20,24) [20,24) [0,4)   [0,4)   [0,4)   [0,4)   [0,4)  
# Levels: [0,4) [4,8) [8,12) [12,16) [16,20) [20,24)

我想对每个时段的length求和:

dt[, sum(length), by=timeSlt]
#    timeSlt       V1
# 1: [20,24)  477.049
# 2:   [0,4) 1069.452

但是所需的输出应该是

y = data.table(timeSlt=levels(dt[, timeSlt]), sumLength=c(1069.452, 0, 0, 0, 0, 477.049))
#    timeSlt sumLength
# 1:   [0,4)  1069.452
# 2:   [4,8)     0.000
# 3:  [8,12)     0.000
# 4: [12,16)     0.000
# 5: [16,20)     0.000
# 6: [20,24)   477.049
如果没有事件发生,

排序timeSlt,相应的length总和填充为0。

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:5)

我们可以根据'{1}}'timeSlt'加入on新创建的data.table,然后按'timeSlt'分组,并获取'{1}}'length'。< / p>

levels

sum选项

dt[setDT(list(timeSlt= levels(dt$timeSlt))), on='timeSlt'
       ][, list(sumLength=sum(length, na.rm=TRUE)), by = timeSlt]
#   timeSlt sumLength
#1:   [0,4)  1069.452
#2:   [4,8)     0.000
#3:  [8,12)     0.000
#4: [12,16)     0.000
#5: [16,20)     0.000
#6: [20,24)   477.049

答案 1 :(得分:1)

也可以使用tapply。

res <- tapply(dt$length, dt$timeSlt, sum)
res
#    [0,4)    [4,8)   [8,12)  [12,16)  [16,20)  [20,24) 
# 1069.452       NA       NA       NA       NA  477.049 
data.frame(timeSlt=names(res), sumLength=res, row.names=1:length(res))
#   timeSlt sumLength
# 1   [0,4)  1069.452
# 2   [4,8)        NA
# 3  [8,12)        NA
# 4 [12,16)        NA
# 5 [16,20)        NA
# 6 [20,24)   477.049

答案 2 :(得分:0)

这是一种dplyr方式

library(tidyr)
library(dplyr)
library(rex)

time_slot_regex = rex("[",
                      digits %>% capture,
                      ",",
                      digits %>% capture,
                      ")")

time_slots = 
  data_frame(start = 0:5 * 4,
             end = start + 4,
             length = 0)

dt %>%
  extract(timeSlt,
          c("start", "end"),
          time_slot_regex,
          convert = TRUE) %>%
  bind_rows(time_slots) %>%
  group_by(start, end) %>%
  summarize(sum_length = sum(length))