How to fill NAs with LOCF by factors in data frame, split by country

后端 未结 8 1974
谎友^
谎友^ 2020-12-08 05:01

I have the following data frame (simplified) with the country variable as a factor and the value variable has missing values:

country value
AUT     NA
AUT            


        
8条回答
  •  悲&欢浪女
    2020-12-08 05:38

    I'm a little late to this conversation, but here is a data.table way, which will be much faster for larger data sets:

    library(zoo)
    library(data.table)
    
    # Convert to data table
    setDT(data)
    
    data[, value := na.locf(value, na.rm = FALSE), by = country]
    
    data
       country  value
    1:     AUT     NA
    2:     AUT      5
    3:     AUT      5
    4:     AUT      5
    5:     GER     NA
    6:     GER     NA
    7:     GER      7
    8:     GER      7
    9:     GER      7
    
    # And if you want to convert "data" back to a data frame...
    setDF(data)
    

提交回复
热议问题