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

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

    You simply need to split by country, then a do either a zoo::na.locf() or na.fill, filling to the right. Here is an example explicitly showing the three-component arg syntax of na.fill:

    library(plyr)
    library(zoo)
    
    data <- data.frame(country=c("AUT", "AUT", "AUT", "AUT", "GER", "GER", "GER", "GER", "GER"), value=c(NA, 5, NA, NA, NA, NA, 7, NA, NA))
    
    # The following is equivalent to na.locf
    na.fill.right <- function(...) { na.fill(..., list(left=NA,interior=NA,right="extend")) }
    
    ddply(data, .(country), na.fill.right)
    
      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
    

提交回复
热议问题