在聚合函数中创建序列

时间:2015-06-11 15:27:40

标签: r data.table aggregate-functions

如何使用data.table在聚合中创建先前行的内容序列? e.g。

pets <- data.table(id = 1 : 12, pet = c(rep("dog", 1), "cat", "cat", rep("dog", 3), "cat", rep("dog", 4), "cat"), town = c(rep("boston", 9), rep("sf", 3)), names = letters[1:12])


    id pet   town names
 1:  1 dog boston     a
 2:  2 cat boston     b
 3:  3 cat boston     c
 4:  4 dog boston     d
 5:  5 dog boston     e
 6:  6 dog boston     f
 7:  7 cat boston     g
 8:  8 dog boston     h
 9:  9 dog boston     i
10: 10 dog     sf     j
11: 11 dog     sf     k
12: 12 cat     sf     l

会导致,

    id pet   town names prior
 1:  1 dog boston     a    NA
 2:  2 cat boston     b    a
 3:  3 cat boston     c    a 
 4:  4 dog boston     d    NA
 5:  5 dog boston     e    NA
 6:  6 dog boston     f    NA
 7:  7 cat boston     g d e f
 8:  8 dog boston     h    NA
 9:  9 dog boston     i    NA
10: 10 dog     sf     j    NA
11: 11 dog     sf     k    NA
12: 12 cat     sf     l   j k

试过了,

> pets[, .SD[, prior := paste(names[-.N], collapse = ' '), .(group=cumsum(c(0,diff(pet == "cat")) < 0))][pet != "cat", prior := ''] , by = town]

但是,这导致,

Error in `[.data.table`(.SD, , `:=`(prior, paste(names[-.N], collapse = " ")),  : 
.SD is locked. Using := in .SD's j is reserved for possible future use; a tortuously flexible way to modify by group. Use := in j directly to modify by group by reference.

1 个答案:

答案 0 :(得分:1)

您不需要.SD[,,并且分组也应该基于&#39; town&#39;从预期的结果显示。还假设您要使用''

填充缺失的值
pets[, prior:=paste(names[pet!='cat'], collapse= ' '),
  .(group=cumsum(c(0, diff(pet=='cat')) <0), town)][pet!='cat', 
       prior := '', by=town]
#     id pet   town names prior
#  1:  1 dog boston     a      
#  2:  2 cat boston     b     a
#  3:  3 cat boston     c     a
#  4:  4 dog boston     d      
#  5:  5 dog boston     e      
#  6:  6 dog boston     f      
#  7:  7 cat boston     g d e f
#  8:  8 dog boston     h      
#  9:  9 dog boston     i      
# 10: 10 dog     sf     j      
# 11: 11 dog     sf     k      
# 12: 12 cat     sf     l   j k
相关问题