简体   繁体   中英

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

> 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(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

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

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 . Here is a minimal example using some numeric data

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

In base 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

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

giving the same result.

Heres an answer using a for loop:

# 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]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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