Search code examples
rlinear-interpolation

Multiple columns with NAs, impute NAs by grouped linear interpolation


I have several columns with NAs, I want to impute the NAs of columns based on the state the row belongs to using linear interpolation.

I'd also like in the same code to create new column names that are interp_[variable name] formatted.


Solution

  • We can do a group by interpolation

    library(dplyr)
    library(forecast)
    df1 %>%
         group_by(state) %>%
         mutate_at(vars(-group_cols()), list(interp= ~ na.interp(.)))
    

    If the columns are not all numeric, use mutate_if(is.numeric, list(interp= ~ na.interp(.)))