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

后端 未结 8 1956
谎友^
谎友^ 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 06:04

    A modern version of the ddply solution is to use the package dplyr:

    library(dplyr)
    DF %>%
      group_by(county) %>% 
      mutate(value = na.locf(value, na.rm = F))      
    

提交回复
热议问题