I want to replace NA values in val2
in each row with the mean of val
corresponding to that ID
column. Any easy (tidyverse) way to do this?
Also, I want to know how to replace it by mean(na.rm=TRUE)
of val2
values itself by categories (For ex: for row 6 & 9 val2 will be replace by 4, mean(na.rm=TRUE)
of val2
(for ID==c
))
For the dataframe in this image :
R code:
df <- data.frame(ID=c("a","b","c","a","b","c","a","b","c"),
val=c(seq(1:9)), val2=c(1,2,4,NA,5,NA,7,NA,NA))