Reputation: 169
instead a for loop, is there a short and efficient way to assign nearest available value to NA in that column in a dataframe? E.g, there's a dataframe DF
DF <- data.frame(x = c(1:10), y1 = c(0, 10, NA,5, 20,30,7,8,9,11), y2 = c(NA, 0, NA,5, 20,30,7,8,NA,NA), z = c(95,94,90:87,88,89,90,91))
x y1 y2 z
1 1 0 NA 95
2 2 10 0 94
3 3 NA NA 90
4 4 5 5 89
5 5 20 20 88
6 6 30 30 87
7 7 7 7 88
8 8 8 8 89
9 9 9 NA 90
10 10 11 NA 91
And the final DF should be
x y1 y2 z
1 1 0 0 95
2 2 10 0 94
3 3 10 0 90
4 4 5 5 89
5 5 20 20 88
6 6 30 30 87
7 7 7 7 88
8 8 8 8 89
9 9 9 8 90
10 10 11 8 91
Thanks in advance!
Upvotes: 2
Views: 165