I have a data frame (datadf) with 3 columns, 'x', 'y and z. Several values ββof "x" are missing ( NA ). 'y' and 'z' are not measured variables.
xyz 153 a 1 163 b 1 NA d 1 123 a 2 145 e 2 NA c 2 NA b 1 199 a 2
I have another data frame (imputeddf) with the same three columns:
xyz 123 a 1 145 a 2 124 b 1 168 b 2 123 c 1 176 c 2 184 d 1 101 d 2
I want to replace NA in 'x' in 'datadf' with the values ββfrom 'imputeddf', where 'y' and 'z' match between two datasets (each combination of 'y' and 'z' has its own value 'x', to fill in).
Desired Result:
xyz 153 a 1 163 b 1 184 d 1 123 a 2 145 e 2 176 c 2 124 b 1 199 a 2
I am trying things like:
finaldf <- datadf finaldf$x <- if(datadf[!is.na(datadf$x)]){ddply(datadf, x=imputeddf$x[datadf$y == imputeddf$y & datadf$z == imputeddf$z])}else{datadf$x}
but it does not work.
What is the best way to populate NA when using my imputed df value?
source share