[R] Subsetting dataframes based on column names

Henrique Dallazuanna wwwhsd at gmail.com
Wed Sep 23 02:18:32 CEST 2009


You can use intersect also:

dat.a[intersect(names(dat.a), names(dat.b))]


On Tue, Sep 22, 2009 at 6:58 PM, Corey Sparks <corey.sparks at utsa.edu> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O




More information about the R-help mailing list