Dear Christopher, Try this: merge(x,y,all=TRUE)
HTH, Jorge On Wed, Jul 1, 2009 at 2:51 PM, Hane, Christopher A < christopher.h...@ingenixconsulting.com> wrote: > > Hello, > > I'm trying to vectorize some assignment statements using match(), but > can't seem to get it correct. > > I have 2 data frames each with a key column of unique values. I want to > copy a column from one frame to another where the key values are the > same. The data frames are not the same length, and the set of keys is > non-overlapping (each frame has keys not in the other). > > Example: > x <- data.frame(id=c(1,3,4,6,7,9,10), r=runif(7)) > > x > id r > 1 1 0.4243219 > 2 3 0.2389127 > 3 4 0.7094532 > 4 6 0.2053836 > 5 7 0.9630027 > 6 9 0.1218458 > 7 10 0.9183175 > > y <- data.frame(id=c(1,2,4,7,8,9,10,11,12)) > > y > id > 1 1 > 2 2 > 3 4 > 4 7 > 5 8 > 6 9 > 7 10 > 8 11 > 9 12 > > I want to copy the x$r values to y to obtain > >y > id r > 1 1 0.4243219 > 2 2 NA > 3 4 0.7094532 > 4 7 0.9630027 > 5 8 NA > 6 9 0.1218458 > 7 10 0.9183175 > 8 11 NA > 9 12 NA > > xIdx <- match(x$id,y$id) yields a vector of length x, so it cannot be > used directly to copy to y. > > Any help in vectorizing this assignment would be most welcome. > > Chris > > This e-mail, including attachments, may include confidential and/or > proprietary information, and may be used only by the person or entity > to which it is addressed. If the reader of this e-mail is not the intended > recipient or his or her authorized agent, the reader is hereby notified > that any dissemination, distribution or copying of this e-mail is > prohibited. If you have received this e-mail in error, please notify the > sender by replying to this message and delete this e-mail immediately. > > ______________________________________________ > R-help@r-project.org mailing list > 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]] ______________________________________________ R-help@r-project.org mailing list 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.