Reputation: 53
Here is some sample data:
dat <- data.frame(col0 = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
col1 = c(NA, 100, 100, NA, 200, 200, NA, 300, 300),
col2 = c(1, 2, 3, 1, 2, 3, 1, 2, 3))
I would like to change any NA value in col1 when col2 = 1, with the value that succeeds the NA in col1.
The best I can figure out is
dat <- dat %>%
mutate(col1 = replace(col1, which(is.na(col1) &
col2 == 1), 100))
But I couldn't figure out how to get the next value of col1...
Ideally, the solution would use tidyverse.
My actual dataset is quite large so a replacing NA in col1 with c(100, 200, 300) would not be an efficient way to proceed.
Upvotes: 2
Views: 78
Reputation: 887048
An option using na.locf
library(zoo)
dat$col1 <- na.locf(dat$col1, fromLast = TRUE)
dat$col1
#[1] 100 100 100 200 200 200 300 300 300
Upvotes: 1
Reputation: 39154
We can use fill
from the tidyr
package.
library(tidyr)
dat2 <- fill(dat, col1, .direction = "up")
dat2
# col0 col1 col2
# 1 1 100 1
# 2 1 100 2
# 3 1 100 3
# 4 2 200 1
# 5 2 200 2
# 6 2 200 3
# 7 3 300 1
# 8 3 300 2
# 9 3 300 3
Upvotes: 2