I have a categorical variable with three levels (A
, B
, and C
).
I also have a continuous variable with some missing values on it.
I would like to replace the NA
values with the mean of its group. This is, missing observations from group A
has to be replaced with the mean of group A
.
I know I can just calculate each group's mean and replace missing values, but I'm sure there's another way to do so more efficiently with loops.
A <- subset(data, group == "A")
mean(A$variable, rm.na = TRUE)
A$variable[which(is.na(A$variable))] <- mean(A$variable, na.rm = TRUE)
Now, I understand I could do the same for group B
and C
, but perhaps a for
loop (with if
and else
) might do the trick?