How to fill NA with median?

前端 未结 6 1310
野性不改
野性不改 2020-12-05 08:44

Example data:

set.seed(1)
df <- data.frame(years=sort(rep(2005:2010, 12)), 
                 months=1:12, 
                 value=c(rnorm(60),NA,NA,NA,NA,         


        
6条回答
  •  情书的邮戳
    2020-12-05 09:06

    This is a way using plyr, it is not very pretty but I think it does what you want:

    library("plyr")
    
    # Make a separate dataframe with month as first column and median as second:
    medDF <- ddply(df,.(months),summarize,median=median(value,na.rm=TRUE))
    
    # Replace `NA` values in `df$value` with medians from the second data frame
    # match() here ensures that the medians are entered in the correct elements.
    df$value[is.na(df$value)] <- medDF$median[match(df$months,medDF$months)][is.na(df$value)]
    

提交回复
热议问题