I'd like some help to compute lag mean of a column just if a value is NA
Follow the example
library(tidyverse)
iname <- rep('a', 10)
itime <- 1:10
ival <- c(1, 2, 3, NA, 5, 6, NA, 8, 9, 10)
ds <- tibble(iname, itime, ival)
ds
# A tibble: 10 × 3
iname itime ival
<chr> <int> <dbl>
1 a 1 1
2 a 2 2
3 a 3 3
4 a 4 NA
5 a 5 5
6 a 6 6
7 a 7 NA
8 a 8 8
9 a 9 9
10 a 10 10
ds %>%
group_by(iname) %>%
mutate(...) -> result
result
# A tibble: 10 × 3
iname itime ival
<chr> <int> <dbl>
1 a 1 1
2 a 2 2
3 a 3 3
4 a 4 2
5 a 5 5
6 a 6 6
7 a 7 3.16
8 a 8 8
9 a 9 9
10 a 10 10
The itime==4
has an empty value in ival
, so in the result, this value were calculated using the previous values mean(1,2,3)
.
In the itime==7
I don't really care if the computation includes or not the values imputed by the mean. But both solutions are welcome, cos it has some difference.
> mean(c(1, 2, 3, 2, 5, 6))
[1] 3.166667
> mean(c(1, 2, 3, 5, 6))
[1] 3.4
I found an equivalent question here Use tidyverse to replace NA with mean of data, by group
Thanks in advance