如何根据R中的相同ID汇总历史数据

时间:2017-09-13 20:05:10

标签: 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
------------------
2  | e    |
------------------
2  | f    | e
-----------------
2  | g    | e,f

我试图在R中使用滞后。但是,它对这个不适用。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

class categoryAdmin(admin.ModelAdmin):
    list_display = ('category_name','category_desc')
    filter_horizontal = ('user',)
    readonly_fields = ('allowed_module_names',)

    def allowed_module_names(self, instance):
        modules = []
        module_names = instance.category.values_list("module_name")
        print(module_names)

数据

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

答案 1 :(得分:0)

以下是使用data.table

的选项
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