Cero
Cero

Reputation: 35

Row operation in R to populate column based on prior row and other variables?

I have a data frame like this:

Index A B Y
1 2 1 1
2 2 1 NA
3 2 1 NA
4 2 1 NA

I want to replace the NA's in 'Y' based on a function of A, B, and the prior value of Y such that Y = Prior Y + A - B

Therefore the final result should be:

Index A B Y
1 2 1 1
2 2 1 2
3 2 1 3
4 2 1 4

This seems like a simple problem, but between all of the various functions out there (apply, reduce, accumulate, lag, etc) I'm not sure what the best approach is. Hoping for a solution with dplyr. Thank you!

Upvotes: 2

Views: 62

Answers (1)

Iroha
Iroha

Reputation: 34761

You can use cumsum() on the combined vector c(Y[1], (A - B)[-1]):

library(dplyr)

dat %>%
  mutate(Y = cumsum(c(Y[1], (A - B)[-1])))

# A tibble: 4 × 4
  Index     A     B     Y
  <dbl> <dbl> <dbl> <dbl>
1     1     2     1     1
2     2     2     1     2
3     3     2     1     3
4     4     2     1     4

Upvotes: 4

Related Questions