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

John Fox jfox at mcmaster.ca
Sat Feb 11 04:13:18 CET 2006


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 at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jeffrey Moore
> Sent: Friday, February 10, 2006 9:23 PM
> To: r-help at stat.math.ethz.ch
> Cc: colchero at 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 at duke.edu
> *****************************************
>  
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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




More information about the R-help mailing list