2015-06-11 71 views
1

如何使用data.table創建聚合中先前行的內容序列?例如在聚合函數中創建序列

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

你不需要.SD[,和分組應也基於「城鎮」從預期的結果顯示。此外,假設您要填寫缺失值''

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 
+0

如果兩隻貓相互跟隨,則包括例如前一隻貓的名稱寵物< - data.table(id = 1:12,pet = c(rep(「dog」,1),「cat」,「cat」,rep(「dog」,3), 「cat」 (「dog」,4),「cat」),town = c(rep(「boston」,9),rep(「sf」,3)),names = letters [1:12])' – Stereo

+0

這個例子。 – Stereo

+0

@Anton我更新了帖子。請檢查 – akrun