Forward and reverse frame fill data in R

I have a data frame with data as follows

Col1    Col2 
20      NA    
25      NA     
15      NA
NA      10
NA      15

etc. I want to change it as follows

Col1     Col2
20        10
25        10
15        10
15        10
15        15

Basically for forwarding or returning an NA value with the first non-NA value. I tried the option Move the last observation of the factor back and forth in the row group to R , but could not get it to work ... Thanks in advance!

+4
source share
1 answer

We can do this with help na.locffromzoo

library(zoo)
na.locf(na.locf(df1), fromLast = TRUE)
#  Col1 Col2
#1   20   10
#2   25   10
#3   15   10
#4   15   10
#5   15   15
+1
source

Source: https://habr.com/ru/post/1672764/


All Articles