I have the following data frame:
library(tidyverse)
v1 = c(1 , NA, 3, 5, NA, NA, 2, 12, NA, 5, NA, 0, 1, 2, 6, 8)
alt = rnorm(length(v1), 0, 1)
tb = tibble(v1, alt)
print(tb)
A tibble: 16 × 2
v1 alt
<dbl> <dbl>
1 1 0.495
2 NA 0.726
3 3 0.667
4 5 0.955
5 NA -1.68
6 NA -1.21
7 2 -1.96
8 12 1.47
9 NA 0.372
10 5 1.07
11 NA 0.531
12 0 0.102
13 1 1.34
14 2 0.0872
15 6 -0.391
16 8 -0.250
I need to fill NAs in v1 using the mutate. The idea is that when there is one NA in v1, it will be filled by the multiplication between the variable alt and the value of v1 prior to the NA.
I solve this using loop for, but this may take time to depend on the data set.
for (i in 1:length(v1)) {
if( is.na(tb[i, 'v1']) ){
tb[i, 'v1'] = tb[i-1, 'v1']*tb[i, 'alt']
}
}
This yields:
A tibble: 16 × 2
v1 alt
<dbl> <dbl>
1 1 0.495
2 0.726 0.726
3 3 0.667
4 5 0.955
5 -8.38 -1.68
6 10.1 -1.21
7 2 -1.96
8 12 1.47
9 4.47 0.372
10 5 1.07
11 2.65 0.531
12 0 0.102
13 1 1.34
14 2 0.0872
15 6 -0.391
16 8 -0.250
My question is: How do I fill NAs using my condition and mutate or others dplyr verbs ?