'Exchange NA with mean of previous and next value
I searched for an answer to how to exchange NAs with the mean of the previous and next values in a DataFrame for specifically one column. But I didn't find an answer which shows how to do this on base R with the addition that NAs can be next to each other.
the DataFrame:
name number
1 John 56
2 Garry NA
3 Carl 70
4 Doris 96
5 Wendy NA
6 Louis NA
7 Becky 40
whished output:
name number
1 John 56
2 Garry 63
3 Carl 70
4 Doris 96
5 Wendy 68
6 Louis 68
7 Becky 40
Solution 1:[1]
In Base R you could do:
idx <- is.na(df$number)
df$number[idx] <- 0
b <- with(rle(df$number), rep(stats::filter(values, c(1,0,1)/2), lengths))
df$number[idx] <- b[idx]
df
name number
1 John 56
2 Garry 63
3 Carl 70
4 Doris 96
5 Wendy 68
6 Louis 68
7 Becky 40
Solution 2:[2]
within(df1, number.fill <-
rowMeans(cbind(ave(number, cumsum(!is.na(number)),
FUN=function(x) x[1]),
rev(ave(rev(number), cumsum(!is.na(rev(number))),
FUN=function(x) x[1])))))
#> name number number.fill
#> 1 John 56 56
#> 2 Garry NA 63
#> 3 Carl 70 70
#> 4 Doris 96 96
#> 5 Wendy NA 68
#> 6 Louis NA 68
#> 7 Becky 40 40
Data:
read.table(text = "name number
John 56
Garry NA
Carl 70
Doris 96
Wendy NA
Louis NA
Becky 40",
header = T, stringsAsFactors = F) -> df1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | onyambu |
Solution 2 | M-- |