简体   繁体   English

R dplyr 根据以前的值和另一列的值添加值

[英]R dplyr add values based on previous value and value from another column

I have a dataframe我有一个数据框

> df
  A B
1 a x
2 b y
3 c z
4 d n
5 e m

I would like the add up the previous value in column A with the current value in column B to replace the current column A, so that the desired output becomes我希望将 A 列中的先前值与 B 列中的当前值相加以替换当前 A 列,以便所需的输出变为

> df
          A B
1         a x
2       a+y y
3     a+y+z z
4   a+y+z+n n
5 a+y+z+n+m m

Code to create the dataframe创建数据框的代码

df = data.frame(A = c('a','b','c', 'd', 'e'), B = c('x', 'y', 'z', 'n', 'm'))

I wrote for loop我写了for循环

for(i in df){
  df$A = lag(df$A) + df$B
}

but it did not work但它没有用

Edit: The actual values are numeric.编辑:实际值是数字。 I use letters for you to read it quickly.我用字母让你快速阅读。 (And perhaps I shouldn't!) (也许我不应该!)

We can use Reduce with accumulate = TRUE我们可以使用Reduceaccumulate = TRUE

Reduce(function(x, y) paste(x, y, sep = "+"), df$B[-1], accumulate = TRUE, 
       init = df$A[1])
#[1] "a"         "a+y"       "a+y+z"     "a+y+z+n"   "a+y+z+n+m"

Similarly, we can also use accumulate from purrr同样,我们也可以使用来自purrr accumulate

library(dplyr)
library(purrr)

df %>% mutate(A = accumulate(B[-1], paste, sep = "+", .init = first(A)))

#          A B
#1         a x
#2       a+y y
#3     a+y+z z
#4   a+y+z+n n
#5 a+y+z+n+m m

data数据

df <- data.frame(A = c('a','b','c', 'd', 'e'), B = c('x', 'y', 'z', 'n', 'm'), 
     stringsAsFactors = FALSE)

You can use cumsum .您可以使用cumsum Here is a minimal example using some numeric data这是使用一些numeric数据的最小示例

df <- data.frame(A = 1:5, B = 6:10)

In base R在基础 R

transform(df, A = A[1] + cumsum(c(0, B[-1])))
#   A  B
#1  1  6
#2  8  7
#3 16  8
#4 25  9
#5 35 10

Or using dplyr或者使用dplyr

library(dplyr)
df %>% mutate(A = A[1] + cumsum(c(0, B[-1])))

giving the same result.给出相同的结果。

Heres an answer using a for loop:这是使用for循环的答案:

# need to make sure they are not factors
df = data.frame(A = c('a','b','c', 'd', 'e'), 
                B = c('x', 'y', 'z', 'n', 'm'),
                stringsAsFactors = F)

# start at 2, not 1, then get the previous row within the loop itself
for (i in 2:nrow(df)){
  df$A[i] <- paste0(df$A[i-1], '+', df$B[i])
}

If you want this to work with numeric data, then use如果您希望它与数字数据一起使用,请使用

for (i in 2:nrow(df)){
  df$A[i] <- df$A[i-1] + df$B[i]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM