I have a table in which there is some missing data that I'd like to replace with the mean of the some other, related data, based on certain conditions. I have some toy data to show the problem below:
var1 var2 var3
123.1 2.1 113
166.5 2.1 113
200.3 2.1 112
NA 2.1 113
NA 2.1 NA
212.1 3.3 112
... ... ...
What I'd like to do is be able to to fill in the NA values for var1
with the mean of va1
in the case where they both have the same var2
and var3
.
ie, for the first NA in the var1
column, this matches on both var2 and var3 with the 1st and 2nd entries, so this would get the value of (123.1 + 166.5) / 2 .
The second NA in the var1
column is missing the var3
information so would be given the mean of all the other var1
values where var2 = 2.1.
I'm relatively new to R and I can't seem to get the conditional logic correct - thanks in advance!
Hmm... I don't think that's what you want, but to do that: