如何总结基于R中
问题描述:
相同ID的历史数据我有数据:如何总结基于R中
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.然而,这对于这个不适用。谁能帮忙?
答
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
DATA
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))
答
下面是使用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
它完美运作。谢谢! – Lily