> On Aug 24, 2016, at 11:48 AM, Christofer Bogaso <bogaso.christo...@gmail.com> > wrote: > > Hello again, > > Let say I have a data.frame which I call as reference data frame : > > Ref = data.frame(c("a", "d", "c", "e", "f", "x"), matrix(NA, 6, 5)) > colnames(Ref) = c("a1", "a2", "a3", "a4", "a5", "a6") > Ref > > Now I have another data.frame, which I call as value data frame : > > Value = data.frame(c("x", "c"), matrix(1:10, 2, 5)) > colnames(Value) = c("a1", "b2", "b3", "b4", "b5", "b6") > Value > > Now I need to insert the values of my 'Value' data frame into my 'Ref' > data frame, according to the order of the column of 'a1' of 'Ref'. > > For example, the NA values of last row of Ref will be (1, 3, 5, 7, > 9) and 3rd row of Ref will be (2, 4, 6, 8, 10). Basically I am > matching the "a1" column of both my data frames. If there is no match > found then corresponding row of Ref will remain unchanged. > > Is there any R way to perform the same programmatically. In reality > both my data frames are quite big, so looking for some automated way > to perform the same. >
> Ref[ match(Value$a1, Ref$a1), 2:6] <- Value[ Value$a1 %in% Ref$a1, 2:6] > Ref a1 a2 a3 a4 a5 a6 1 a NA NA NA NA NA 2 d NA NA NA NA NA 3 c 2 4 6 8 10 4 e NA NA NA NA NA 5 f NA NA NA NA NA 6 x 1 3 5 7 9 The `match` function creates an index vector for the placement, and the `%in%` expression restricts the rows to items which will have a match on the LHS of the assignment. -- David. > Thanks for your time. > > Regards, > > ______________________________________________ > R-help@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. David Winsemius Alameda, CA, USA ______________________________________________ R-help@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.