[英]How to add new column in R data frame showing sum of a value in a current row and a prior row, if certain conditions are met in the 2 rows?
Suppose you have a data frame of columns "a" and "b" with the values shown below, generated with df <- data.frame(a=c(0, 1, 2, 2, 3), b=c(1, 3, 8, 9, 4))
.假设您有一个由“a”和“b”列组成的数据框,其值如下所示,由
df <- data.frame(a=c(0, 1, 2, 2, 3), b=c(1, 3, 8, 9, 4))
。 Suppose you want to add a column "c", whereby if a value in "a" equals the value in the immediately preceding row in col "a", then the corresponding row values in col "b" are summed;假设您要添加“c”列,如果“a”中的值等于“a”列中前一行的值,则将“b”列中的相应行值相加; otherwise a 0 value is shown.
否则显示 0 值。 A column "c" is added to the below to illustrate what I'm trying to do:
下面添加了一个“c”列来说明我正在尝试做的事情:
a b add col c
1 0 1 0
2 1 3 0
3 2 8 0
4 2 9 17 (since the values in col "a" rows 3 and 4 are equal, add the values in col b rows 3 and 4)
5 3 4 0
What is the easiest way to do this in native R?在本机 R 中执行此操作的最简单方法是什么?
We can use我们可以用
df %>% group_by(a) %>% mutate(c = if(n() > 1) sum(b) else 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.