All - I have a data frame data.a ID valueA valueB 6 12 12 17 15 14 58 18 16 98 11 12 73 19 20 84 19 14 58 20 14 24 11 12 81 15 16 21 15 14 62 14 12 67 13 14 78 13 17 35 10 13 13 11 15 14 17 18 85 16 15 35 13 9 18 15 16 and a data frame data.b ID valueA valueB 6 84 21 78 14 I'd like to have R find the data.b$ID in data.a$ID and insert the corresponding data.a$valueA and data.a$valueB into the appropriate columns in data.b. How can I do this? Thanks for you help. SR Steven H. Ranney
Have a look at ?merge John Kane Kingston ON Canada> -----Original Message----- > From: steven.ranney at gmail.com > Sent: Tue, 27 Nov 2012 15:17:58 -0700 > To: r-help at r-project.org > Subject: [R] Finding values in one column and > > All - > > I have a data frame > > data.a > ID valueA valueB > 6 12 12 > 17 15 14 > 58 18 16 > 98 11 12 > 73 19 20 > 84 19 14 > 58 20 14 > 24 11 12 > 81 15 16 > 21 15 14 > 62 14 12 > 67 13 14 > 78 13 17 > 35 10 13 > 13 11 15 > 14 17 18 > 85 16 15 > 35 13 9 > 18 15 16 > > and a data frame > > data.b > ID valueA valueB > 6 > 84 > 21 > 78 > 14 > > I'd like to have R find the data.b$ID in data.a$ID and insert the > corresponding data.a$valueA and data.a$valueB into the appropriate > columns in data.b. > > How can I do this? > > Thanks for you help. > > SR > Steven H. Ranney > > ______________________________________________ > R-help at 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.____________________________________________________________ Publish your photos in seconds for FREE TRY IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if4
Hi, Try ?merge(), ?join() from library(plyr) data.a<-read.table(text=" ID??? valueA??? valueB 6??? 12??? 12 17??? 15??? 14 58??? 18??? 16 98??? 11??? 12 73??? 19??? 20 84??? 19??? 14 58??? 20??? 14 24??? 11??? 12 81??? 15??? 16 21??? 15??? 14 62??? 14??? 12 67??? 13??? 14 78??? 13??? 17 35??? 10??? 13 13??? 11??? 15 14??? 17??? 18 85??? 16??? 15 35??? 13??? 9 18??? 15??? 16 ",sep="",header=TRUE) data.b<-read.table(text=" ID?? 6?????? 84?????? 21?????? 78?????? 14??????? ",sep="",header=TRUE) library(plyr) join(data.a,data.b,by="ID",type="inner") #? ID valueA valueB #1? 6???? 12???? 12 #2 84???? 19???? 14 #3 21???? 15???? 14 #4 78???? 13???? 17 #5 14???? 17???? 18 A.K. ----- Original Message ----- From: Steven Ranney <steven.ranney at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, November 27, 2012 5:17 PM Subject: [R] Finding values in one column and All - I have a data frame data.a ID??? valueA??? valueB 6??? 12??? 12 17??? 15??? 14 58??? 18??? 16 98??? 11??? 12 73??? 19??? 20 84??? 19??? 14 58??? 20??? 14 24??? 11??? 12 81??? 15??? 16 21??? 15??? 14 62??? 14??? 12 67??? 13??? 14 78??? 13??? 17 35??? 10??? 13 13??? 11??? 15 14??? 17??? 18 85??? 16??? 15 35??? 13??? 9 18??? 15??? 16 and a data frame data.b ID??? valueA??? valueB 6??? ??? 84??? ??? 21??? ??? 78??? ??? 14??? ??? I'd like to have R find the data.b$ID in data.a$ID and insert the corresponding data.a$valueA and data.a$valueB into the appropriate columns in data.b. How can I do this? Thanks for you help. SR Steven H. Ranney ______________________________________________ R-help at 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.
Come to think of it the plyr package and the data.table packages also offer similar tools. For large merges (joins) I think the data.table package is much faster. John Kane Kingston ON Canada> -----Original Message----- > From: steven.ranney at gmail.com > Sent: Tue, 27 Nov 2012 15:28:57 -0700 > To: jrkrideau at inbox.com > Subject: Re: [R] Finding values in one column and > > Thanks. > > Soon after I posted this question, I discovered merge(). > > Steven H. Ranney > > > On Tue, Nov 27, 2012 at 3:26 PM, John Kane <jrkrideau at inbox.com> wrote: >> Have a look at ?merge >> >> John Kane >> Kingston ON Canada >> >> >>> -----Original Message----- >>> From: steven.ranney at gmail.com >>> Sent: Tue, 27 Nov 2012 15:17:58 -0700 >>> To: r-help at r-project.org >>> Subject: [R] Finding values in one column and >>> >>> All - >>> >>> I have a data frame >>> >>> data.a >>> ID valueA valueB >>> 6 12 12 >>> 17 15 14 >>> 58 18 16 >>> 98 11 12 >>> 73 19 20 >>> 84 19 14 >>> 58 20 14 >>> 24 11 12 >>> 81 15 16 >>> 21 15 14 >>> 62 14 12 >>> 67 13 14 >>> 78 13 17 >>> 35 10 13 >>> 13 11 15 >>> 14 17 18 >>> 85 16 15 >>> 35 13 9 >>> 18 15 16 >>> >>> and a data frame >>> >>> data.b >>> ID valueA valueB >>> 6 >>> 84 >>> 21 >>> 78 >>> 14 >>> >>> I'd like to have R find the data.b$ID in data.a$ID and insert the >>> corresponding data.a$valueA and data.a$valueB into the appropriate >>> columns in data.b. >>> >>> How can I do this? >>> >>> Thanks for you help. >>> >>> SR >>> Steven H. Ranney >>> >>> ______________________________________________ >>> R-help at 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. >> >> ____________________________________________________________ >> Publish your photos in seconds for FREE >> TRY IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if4 >> >>____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
On 27-11-2012, at 23:17, Steven Ranney wrote:> All - > > I have a data frame > > data.a > ID valueA valueB > 6 12 12 > 17 15 14 > 58 18 16 > 98 11 12 > 73 19 20 > 84 19 14 > 58 20 14 > 24 11 12 > 81 15 16 > 21 15 14 > 62 14 12 > 67 13 14 > 78 13 17 > 35 10 13 > 13 11 15 > 14 17 18 > 85 16 15 > 35 13 9 > 18 15 16 > > and a data frame > > data.b > ID valueA valueB > 6 > 84 > 21 > 78 > 14 > > I'd like to have R find the data.b$ID in data.a$ID and insert the > corresponding data.a$valueA and data.a$valueB into the appropriate > columns in data.b. > > How can I do this?data.b <- data.a[which(data.a$ID %in% data.b$ID), ] Berend