Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中基于相同ID汇总历史数据_R_String_Concatenation_Row - Fatal编程技术网

如何在R中基于相同ID汇总历史数据

如何在R中基于相同ID汇总历史数据,r,string,concatenation,row,R,String,Concatenation,Row,我有以下数据: id |result -------- 1 | a ------- 1 | b ------- 1 | c ------- 2 | e ------- 2 | f ------- 2 | g 下面列出了我真正想要的数据帧: id |result|history ------------------- 1 | a | ------------------- 1 | b | a ------------------ 1 | c | a,b --

我有以下数据:

id |result
--------
1  | a
-------
1  | b
-------
1  | c
-------
2  | e
-------
2  | f
-------
2  | g
下面列出了我真正想要的数据帧:

id |result|history
-------------------
1  | a    | 
-------------------
1  | b    | a
------------------
1  | c    | a,b
------------------
2  | e    |
------------------
2  | f    | e
-----------------
2  | g    | e,f
我试着在R中使用lag。但是,它对这个不起作用。有人能帮忙吗

df$History = unlist(tapply(X = df$result, INDEX = df$id, function(a)
    c("", Reduce(function(x, y) {paste(x, y, sep = ", ")},
                head(a, -1),
                 accumulate = TRUE))))
df
#  id result History
#1  1      a        
#2  1      b       a
#3  1      c    a, b
#4  2      e        
#5  2      f       e
#6  2      g    e, f
数据

df = structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L), result = c("a", 
        "b", "c", "e", "f", "g")), .Names = c("id", "result"),
        class = "data.frame", row.names = c(NA, -6L))
数据

df = structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L), result = c("a", 
        "b", "c", "e", "f", "g")), .Names = c("id", "result"),
        class = "data.frame", row.names = c(NA, -6L))

这里有一个使用
数据的选项。表

library(data.table)
setDT(df1)[, history := Reduce(paste, shift(result, fill = ""), accumulate = TRUE), id]
df1
#   id result history
#1:  1      a        
#2:  1      b       a
#3:  1      c     a b
#4:  2      e        
#5:  2      f       e
#6:  2      g     e f
如果我们需要
作为分隔符

setDT(df1)[, history := c("", Reduce(function(...) paste(..., sep= ","),
            result[-.N], accumulate = TRUE)), id]
df1
#   id result history
#1:  1      a        
#2:  1      b       a
#3:  1      c     a,b
#4:  2      e        
#5:  2      f       e
#6:  2      g     e,f

这里有一个使用
数据的选项。表

library(data.table)
setDT(df1)[, history := Reduce(paste, shift(result, fill = ""), accumulate = TRUE), id]
df1
#   id result history
#1:  1      a        
#2:  1      b       a
#3:  1      c     a b
#4:  2      e        
#5:  2      f       e
#6:  2      g     e f
如果我们需要
作为分隔符

setDT(df1)[, history := c("", Reduce(function(...) paste(..., sep= ","),
            result[-.N], accumulate = TRUE)), id]
df1
#   id result history
#1:  1      a        
#2:  1      b       a
#3:  1      c     a,b
#4:  2      e        
#5:  2      f       e
#6:  2      g     e,f