使用dplyr按组进行累计计数 [英] Using dplyr to get cumulative count by group
本文介绍了使用dplyr按组进行累计计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
提前感谢。我有以下数据:
Thanks in advance. I have the following data:
df <- data.frame(person=c(1,1,1,1,2,2,2,2,3,3,3,3),
neighborhood=c("A","A","A","A","B","B","C","C","D","D","E","F"))
我想生成一个新的列,给出每个人随着面板进度而移动的社区的累积计数。像这样:
I would like to generate a new column that gives the cumulative count of neighborhoods that each person moves through as the panel progresses. Like such:
df2 <- data.frame(person=c(1,1,1,1,2,2,2,2,3,3,3,3),
neighborhood=c("A","A","A","A","B","B","C","C","D","D","E","F"),
moved=c(0,0,0,0,0,0,1,1,0,0,1,2)
)
再次感谢
推荐答案
我们可以使用group by'person',然后通过匹配
创建'移动'使用独特的
值来获取索引并减1。
We can use group by 'person', then create the 'moved' by match
ing the 'neighborhood' with its unique
values to get the index and subtract 1.
df %>%
group_by(person) %>%
mutate(moved = match(neighborhood, unique(neighborhood))-1)
# person neighborhood moved
# <dbl> <fctr> <dbl>
#1 1 A 0
#2 1 A 0
#3 1 A 0
#4 1 A 0
#5 2 B 0
#6 2 B 0
#7 2 C 1
#8 2 C 1
#9 3 D 0
#10 3 D 0
#11 3 E 1
#12 3 F 2
或使用因素
with levels
指定为邻居中的独特
值,强制为整数,并减1。
or use factor
with levels
specified as the unique
values in 'neighborhood', coerce to 'integer' and subtract 1.
df %>%
group_by(person) %>%
mutate(moved = as.integer(factor(neighborhood, levels = unique(neighborhood)))-1)
# person neighborhood moved
# <dbl> <fctr> <dbl>
#1 1 A 0
#2 1 A 0
#3 1 A 0
#4 1 A 0
#5 2 B 0
#6 2 B 0
#7 2 C 1
#8 2 C 1
#9 3 D 0
#10 3 D 0
#11 3 E 1
#12 3 F 2
这篇关于使用dplyr按组进行累计计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文