How to use values ​​from previous row and column

I am trying to create a new variable that is a function of the previous rows and columns. I found the lag () function in dplyr, but it cannot accomplish exactly what I would like.

library(dplyr)
x = data.frame(replicate(2, sample(1:3,10,rep=TRUE)))

   X1 X2
1   1  3
2   2  3
3   2  2
4   1  3
5   2  3
6   2  1
7   3  2
8   1  1
9   1  3
10  2  2

x = mutate(x, new_col = # if x2==1, then the value of x1 in the previous row,
                        # if x2!=1, then 0))

      

My best attempt:

foo = function(x){
    if (x==1){
        return(lag(X1))
    }else{
        return(0)
}

x = mutate(x, new_col=foo(X1))

      

+3


source to share


2 answers


We can use ifelse



x %>% 
  mutate(newcol = ifelse(X2==1, lag(X1), 0))

      

+4


source


In R base, you can use

x$newcol <- (x$X2 == 1) * c(NA, tail(x$X1, -1))

      



(x$X2 == 1)

provides 0s for all elements of X2 not equal to 1, and a multiple of these two will return the lagging values ​​of X1 when X2 == 1

.

+3


source







All Articles