[R] Need help merging two dataframes

Nathaniel nathanielrayl at Hotmail.com
Wed Feb 9 15:09:53 CET 2011


Hi R users,

I am trying to extract some attributes (age, sex, area) from dataframe "AB"
that has 101,269 observations of 28 variables to dataframe "t2" that has 47
observations of 6 variables.  They share a column called "id", which is a
factor with 47 levels.  I want to end up with a dataframe that has 47
observations of 9 variables (the original 6 variables of t2, plus age, sex,
and area). The issue I am having is that in AB has multiple entries for each
id, and so I can't use merge because there is more than one match, so all
possible matches contribute one row each--i.e., this code gives me dataframe
"t3" of 101,269 observations of 33 variables:

>t3<-merge(t2,AB,by="id",all=FALSE)

Dataframe AB (24 variables omitted from example dataframe):

id          sex        age         area
01         male      adult       LP
01         male      adult       LP
01         male      adult       LP
...
02       female    subadult   LP
02       female    subadult   LP
02       female    subadult   LP
02       female    subadult   LP
...           
03       male      subadult    MR
03       male      subadult    MR
03       male      subadult    MR
03       male      subadult    MR
...

Dataframe t2 (5 variables omitted from example dataframe):

id
01
02
03
04
05
06
....       

This is the structure I want for dataframe t3 (5 variables omitted from
example dataframe):

id          sex        age         area
01         male      adult       LP
02       female    subadult   LP
03       male      subadult   MR
...

Hopefully this all makes sense and someone knows a solution.  Thanks in
advance for taking a look at my problem and helping out (I hope!).

Nathaniel
-- 
View this message in context: http://r.789695.n4.nabble.com/Need-help-merging-two-dataframes-tp3297313p3297313.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list