0

I have a dataframe with NA's. I want to fill the NA's with the previous known value, but if no previous value is applicable it should keep the NA.

Example:

df <- data.frame(col1 = c(1,2,3,4,5), col2  = c("a", NA, NA , "b", NA), col3 = c(NA, "B", NA, "C", NA), col4 = c(NA, NA, "1", "2", NA)) 

I have tried the na.locf function of the zoo library

do(na.locf(.))

But this removes the rows having NA's without a previous non NA value. The outcome should be:

col1 col2 col3 col4
1     a    NA   NA
2     a    B    NA
3     a    B    1
4     b    C    2
5     b    C    2

A possible solution might be to apply the na.locf function to all columns and put it in a list resulting in list elements with different row lenght.

df_list <- apply(df, 2, na.locf)

In a second step additional rows should be added on top of / before each element for all the NA's removed. But this I could't get this step working.

Any ideas what is the best approach to tackle the problem?

learneR
  • 53
  • 4

0 Answers0