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

后端 未结 8 1979
谎友^
谎友^ 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:51

    Here's a ddply solution. Try this

    library(plyr)
    ddply(DF, .(country), na.locf)
      country value
    1     AUT  
    2     AUT     5
    3     AUT     5
    4     AUT     5
    5     GER  
    6     GER  
    7     GER     7
    8     GER     7
    9     GER     7
    

    Edit From ddply help you can find that

    .variables:  variables to split data frame by, 
    as quoted variables, a formula or character vector.
    

    so another alternatives to get what you want are:

    ddply(DF, "country", na.locf)
    ddply(DF, ~country, na.locf)
    

    note that replacing .variables with DF$variable is not allowed, that's why you got an error when doing this.

    DF is your data.frame

提交回复
热议问题