9

I am trying to create a new variable which is a function of previous rows and columns. I have found the lag() function in dplyr but it can't accomplish exactly what I would like.

library(dplyr)
set.seed(0)

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

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

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

2 Answers 2

12

We can use ifelse

x %>% 
  mutate(newcol = ifelse(X2==1, lag(X1), 0))
Sign up to request clarification or add additional context in comments.

Comments

5

In base R, you can use

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

(x$X2 == 1) ensures 0s for all elements of X2 not equal to 1, and the multiple of the two terms will return the lagged values of X1 when X2 == 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.