I have this kind of dataframe (df1):
Code ID_CODE value_1 value_2 value_3
HA09U 98_ 10 57.80 NA NA
HA09U 98_ 11 57.80 NA NA
HA09U 98_ 12 57.80 NA NA
MB03L 99_ 10 NA 90.77 NA
MB03L 99_ 11 NA 90.77 NA
MB03L 99_ 12 NA 90.77 NA
KE17P 100_ 10 68.44 76.05 72.01
KE17P 100_ 11 68.44 76.05 72.01
KE17P 100_ 12 68.44 76.05 72.01
I want to replace the value of ALL variables (value_1, value_2 and value_3) of the person that includes NAs by adding the values from my second dataframe (df2):
Code value_1 value_2 value_3
HA09U 90.35 89.84 90.07
MB03L 66.94 79.62 73.77
The result should be:
Code ID_CODE value_1 value_2 value_3
HA09U 98_ 10 90.35 89.84 90.07
HA09U 98_ 11 90.35 89.84 90.07
HA09U 98_ 12 90.35 89.84 90.07
MB03L 99_ 10 66.94 79.62 73.77
MB03L 99_ 11 66.94 79.62 73.77
MB03L 99_ 12 66.94 79.62 73.77
KE17P 100_ 10 68.44 76.05 72.01
KE17P 100_ 11 68.44 76.05 72.01
KE17P 100_ 12 68.44 76.05 72.01
I tried to use some merge and bind_rows function, this one took me the closest:
df1$value_1[df1$Code == df2$Code] <- df2$value_1
But it did not worked and somehow messed up the dataframe.
Using this code
d1 <- left_join(df1, df2, by = "Code" )
Adds three new colums with the values of df2, but the values of KE17P (the person that already has complete data) is missing. So I am really looking for a replacing function.
left_join()fromdplyrwill give you what you want.df3<- left_join(df1, df2, by = "Code" )and it worked so far... but adds the values from df2 by adding three new columns and not replacing the old values in the original columnsdf1before you do theleft_join...