Re: [R] how do I "relate" tables in R?

From: Jeffrey Moore <jemoore_at_duke.edu>
Date: Sat 11 Feb 2006 - 16:48:48 EST


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: jemoore@duke.edu

-----Original Message-----
From: John Fox [mailto:jfox@mcmaster.ca] Sent: Friday, February 10, 2006 10:13 PM To: 'Jeffrey Moore'
Cc: colchero@duke.edu; r-help@stat.math.ethz.ch 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: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Jeffrey Moore
> Sent: Friday, February 10, 2006 9:23 PM
> To: r-help@stat.math.ethz.ch
> Cc: colchero@duke.edu
> 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: jemoore@duke.edu
> *****************************************
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Feb 11 16:54:39 2006

This archive was generated by hypermail 2.1.8 : Mon 13 Feb 2006 - 23:10:09 EST