[R] replacing values of rows with identical row names in two dataframes
Saba Sehrish
sabasehrish at yahoo.com
Sat May 7 01:15:09 CEST 2016
Hi
I have two dataframes(df1, df2) with equal number of columns (1566) but lesser rows in df2 (2772 in df1 and 40 in df2). Row names are
identical in both dataframes (date). I want to replace NAs of df1 with the values of df2 for all those rows having identical row names (date) but
without affecting already existing values in those rows of df1.
Please see below:
df1:
date 11A 11A 21B 3CC 3CC
20040101 100 150 NA NA 140
20040115 200 NA 200 NA NA
20040131 NA 165 180 190 190
20040205 NA NA NA NA NA
20040228 NA NA NA NA NA
20040301 150 155 170 150 160
20040315 NA NA 180 190 200
20040331 NA NA NA 175 180
df2:
date 11A 11A 21B 3CC 3CC
20040131 170 NA NA NA NA
20040228 140 145 165 150 155
20040331 NA 145 160 NA NA
I want the resulting dataframe to be:
df3:
date 11A 11A 21B 3CC 3CC
20040101 100 150 NA NA 140
20040115 200 NA 200 NA NA
20040131 170 165 180 190 190
20040205 NA NA NA NA NA
20040228 140 145 165 150 155
20040301 150 155 170 150 160
20040315 NA NA 180 190 200
20040331 NA 145 160 175 180
If it is possible, I would prefer to use "for loop" and "which" function to achieve the result.
Please guide me in this regard.
Thanks
Saba
More information about the R-help
mailing list