xfactor
xfactor

Reputation: 17

How to convert "for loop" to "apply family functions"

My data frame looks like this. (it has ~ 300,000 rows) I want to do this operation faster and elegantly. How can i overcome this problem. Thanks in advance.

df <- data.frame(
   X = sample(0:1, 10, replace = T),
   Y = sample(10:20, 10, replace = T)
 )

for (i in 2:(nrow(df)))
{
   if (df$X[i] == 0)
   {
      df$X[i] = df$Y[i - 1]
   }
}

Upvotes: 0

Views: 86

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388907

We can try lag

df$X <- ifelse(df$X == 0, lag(df$Y), df$X)

If we need to ignore the first row

df[2:nrow(df), ] <- with(df[2:nrow(df), ], ifelse(X == 0, lag(Y), X))

Upvotes: 2

Related Questions