I couldn't figure this out....
I have a data frame looks like this (only the top 10 rows are shown):
Value Type
NA 3
23 2
54 1
45 1
21 2
55 3
67 3
78 1
10 1
NA 2
Task:
Replace NA with the mean value of its given Type. Ex: The first NA is in Type 3, so I'd like to replace it with the average value in Type 3, that is (55+67)/2= 61
My code:
for (i in 1:nrow(df)){
if(is.na(df[i,"Value"])==TRUE & Type==1){
df[i,"Value"] = mean(with(df, subset(Value, Type==1)))
}
else if (is.na(df[i,"Value"])==TRUE & Type==2){
df[i,"Value"] = mean(with(df, subset(Value, Type==2)))
}
else if (is.na(df[i,"Value"])==TRUE & Type==3){
df[i,"Value"] = mean(with(df, subset(Value, Type==3)))
}
else (df[i,"Value"] = df[i,"Value"])
}
Result
NAs are still observed in the Value column and they are not being replaced by the mean value of its class.
any help is appreciated!