df <- data.frame(category=c("cat1","cat1","cat2","cat1","cat2","cat2","cat1","cat2"),
value=c(1,2,3,4,5,6,7,8))
我想在上面的数据框中添加一个新列,其中列出了value
到该点的元素,按category
分组。在我看来,它看起来像是:
df <- df %>%
group_by(category) %>%
mutate(value_list=list(some_sort_of_cumulative_identity_function(value)))
所以期望的输出是
category value value_list
1 cat1 1 1
2 cat1 2 c(1,2)
3 cat2 3 3
4 cat1 4 c(1,2,4)
5 cat2 5 c(3,5)
6 cat2 6 c(3,5,6)
7 cat1 7 c(1,2,4,7)
8 cat2 8 c(3,5,6,8)
其中value_list的元素是列表。
答案 0 :(得分:3)
我猜这在tidyverse当前是惯用的:
library(purrr)
df %>% group_by(category) %>% mutate(vlist = accumulate(value, c))
# A tibble: 8 x 3
# Groups: category [2]
category value vlist
<fctr> <dbl> <list>
1 cat1 1 <dbl [1]>
2 cat1 2 <dbl [2]>
3 cat2 3 <dbl [1]>
4 cat1 4 <dbl [3]>
5 cat2 5 <dbl [2]>
6 cat2 6 <dbl [3]>
7 cat1 7 <dbl [4]>
8 cat2 8 <dbl [4]>
(感谢@akrun同时发表评论。)
基础R类似物是Reduce(c, value, accumulate=TRUE)
。