na
How to delete columns that contain ONLY NAs?
One way of doing it: df[, colSums(is.na(df)) != nrow(df)] If the count of NAs in a column is equal to the number of rows, it must be entirely NA. Or similarly df[colSums(!is.na(df)) > 0]
replace NA value with the group value
Try ave. It applies a function to groups. Have a look at ?ave for details, e.g.: df$med_card_new <- ave(df$med_card, df$hhold_no, FUN=function(x)unique(x[!is.na(x)])) # person_id hhold_no med_card med_card_new #1 1 1 1 1 #2 2 1 1 1 #3 3 1 NA 1 #4 4 1 NA 1 #5 5 1 NA 1 #6 6 2 0 … Read more