Search code examples
rpandasfillna

What are simple R equivalent of Panda's fillna methods?


In particular I am interested in equivalents of

df.fillna(method='ffill')

and

df.fillna(method='bfill')

Solution

  • We can use na.locf from zoo

    library(zoo)
    na.locf(df)
    

    and for the second case,

    na.locf(df, fromLast=TRUE)
    

    data

    set.seed(24)
    df <- as.data.frame(matrix(sample(c(1:3, NA), 5*4, replace=TRUE), 5, 4))