[R] Subsetting dataframes based on column names

Corey Sparks corey.sparks at UTSA.EDU
Tue Sep 22 23:58:33 CEST 2009


Dear R users,
I am interested in taking the columns from multiple dataframes, the  
problem is that the different dataframes have different combinations  
of the same variable names, here's a simple example:
a<-rep(1:10)
b<-rep(1:10)
c<-rep(21:30)
d<-rep(31:40)

dat.a<-data.frame(a,b,c,d)
names(dat.a)<-c("a", "b", "c", "d")

dat.b<-data.frame(a,c,d)
names(dat.b)<-c("a", "c", "d")

I would like to first see if the names in the larger dataframe match  
those of the smaller (they have the same variables)

names(dat.a)%in%names(dat.b)


Could anyone help with this problem, I would basically like to form a  
subset of the dat.a that matches the variable names in dat.b.  If  
there were only a few variables, this would be easier, but I have  
between 4 and 5 thousand variables in each dataset

Any help would be greatly appreciated.
Best,
Corey

Corey Sparks
Assistant Professor
Department of Demography and Organization Studies
University of Texas at San Antonio
College of Public Policy
501 West Durango Blvd
Monterey Building 2.270C
San Antonio, TX 78207
210 458 3166
corey.sparks 'at' utsa.edu




More information about the R-help mailing list