xfactor - 2 months ago 14

R Question

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]

}

}

Answer

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