replace NA value with the group value

后端 未结 5 729
栀梦
栀梦 2020-11-27 22:15

I have a df as follows which has 20 people across 5 households. Some people within the household have missing data for whether they have a med_card or not. I want to give th

5条回答
  •  悲&欢浪女
    2020-11-27 22:30

    This is late, but if are working on a numeric column, try this:

    require(data.table)
    
    setDT(df)[,'record_year':=mean( med_card,na.rm = T),by = c('hhold_no')]
    

提交回复
热议问题