|
Hi all,
I'm new to the list...pretty new at learning to code in R... Is there a way to relate 2 different arrays in R? Hypothetical example: data1 ID z 1 100 2 250 3 75 4 12 5 89 data2 ID z 1 1 1 1 2 3 4 3 4 5 5 5 etc. Goal is to fill column z in data2 with appropriate z-values from data1 that correspond to a given ID. I'm looking for something akin to a relational database, or a lookup table in Excel. I can construct a simple for-loop (with if else statement) to fill the z-column in data2, but in my case data2 is 150,000 records long, so this approach is not efficient. Thanks for any help Jeff ****************************************** Jeffrey Moore, Ph.D. Postdoctoral Research Scientist Duke Center for Marine Conservation Duke University Marine Laboratory 135 Duke Marine Lab Road Beaufort, NC 28516 Phone: (252) 504-7653 Fax: (252) 504-7689 Email: [hidden email] ***************************************** [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html |
|
Dear Jeff,
Assuming that the column named "z" in the matrix data2 already exists and has arbitrary content (such as 0's or NA's), how about the following? data2[,"z"] <- data1[data2[,"ID"], "z"] I hope this helps, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox -------------------------------- > -----Original Message----- > From: [hidden email] > [mailto:[hidden email]] On Behalf Of Jeffrey Moore > Sent: Friday, February 10, 2006 9:23 PM > To: [hidden email] > Cc: [hidden email] > Subject: [R] how do I "relate" tables in R? > > Hi all, > > I'm new to the list...pretty new at learning to code in R... > > Is there a way to relate 2 different arrays in R? > > Hypothetical example: > > data1 > ID z > 1 100 > 2 250 > 3 75 > 4 12 > 5 89 > > data2 > ID z > 1 > 1 > 1 > 1 > 2 > 3 > 4 > 3 > 4 > 5 > 5 > 5 > etc. > > Goal is to fill column z in data2 with appropriate z-values > from data1 that correspond to a given ID. > > I'm looking for something akin to a relational database, or a > lookup table in Excel. > > I can construct a simple for-loop (with if else statement) to > fill the z-column in data2, but in my case data2 is 150,000 > records long, so this approach is not efficient. > > Thanks for any help > Jeff > > ****************************************** > Jeffrey Moore, Ph.D. > Postdoctoral Research Scientist > Duke Center for Marine Conservation > Duke University Marine Laboratory > 135 Duke Marine Lab Road > Beaufort, NC 28516 > Phone: (252) 504-7653 > Fax: (252) 504-7689 > Email: [hidden email] > ***************************************** > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html |
|
thanks, Andy and John, for suggestions.
worked great. jeff ****************************************** Jeffrey Moore, Ph.D. Postdoctoral Research Scientist Duke Center for Marine Conservation Duke University Marine Laboratory 135 Duke Marine Lab Road Beaufort, NC 28516 Phone: (252) 504-7653 Fax: (252) 504-7689 Email: [hidden email] ***************************************** -----Original Message----- From: John Fox [mailto:[hidden email]] Sent: Friday, February 10, 2006 10:13 PM To: 'Jeffrey Moore' Cc: [hidden email]; [hidden email] Subject: RE: [R] how do I "relate" tables in R? Dear Jeff, Assuming that the column named "z" in the matrix data2 already exists and has arbitrary content (such as 0's or NA's), how about the following? data2[,"z"] <- data1[data2[,"ID"], "z"] I hope this helps, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox -------------------------------- > -----Original Message----- > From: [hidden email] > [mailto:[hidden email]] On Behalf Of Jeffrey Moore > Sent: Friday, February 10, 2006 9:23 PM > To: [hidden email] > Cc: [hidden email] > Subject: [R] how do I "relate" tables in R? > > Hi all, > > I'm new to the list...pretty new at learning to code in R... > > Is there a way to relate 2 different arrays in R? > > Hypothetical example: > > data1 > ID z > 1 100 > 2 250 > 3 75 > 4 12 > 5 89 > > data2 > ID z > 1 > 1 > 1 > 1 > 2 > 3 > 4 > 3 > 4 > 5 > 5 > 5 > etc. > > Goal is to fill column z in data2 with appropriate z-values from data1 > that correspond to a given ID. > > I'm looking for something akin to a relational database, or a lookup > table in Excel. > > I can construct a simple for-loop (with if else statement) to fill the > z-column in data2, but in my case data2 is 150,000 records long, so > this approach is not efficient. > > Thanks for any help > Jeff > > ****************************************** > Jeffrey Moore, Ph.D. > Postdoctoral Research Scientist > Duke Center for Marine Conservation > Duke University Marine Laboratory > 135 Duke Marine Lab Road > Beaufort, NC 28516 > Phone: (252) 504-7653 > Fax: (252) 504-7689 > Email: [hidden email] > ***************************************** > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html |
|
In reply to this post by John Fox
?merge
-- ********************************* Hans Gardfjell Ecology and Environmental Science Umeå University 90187 Umeå, Sweden email: [hidden email] phone: +46 907865267 mobile: +46 705984464 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html |
| Powered by Nabble | Edit this page |
