[R] replacing values of rows with identical row names in two dataframes

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sat May 7 01:30:41 CEST 2016


Why would you want to use a for loop?  Is this homework? 
-- 
Sent from my phone. Please excuse my brevity.

On May 6, 2016 4:15:09 PM PDT, Saba Sehrish via R-help <r-help at r-project.org> wrote:
>
>
>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
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]



More information about the R-help mailing list