简体   繁体   English

从多个数据框中提取具有相同名称的列[R]

[英]Extract columns with same names from multiple data frames [R]

I am dealing with about 10 data frames that have the same column names, but different number of rows. 我正在处理大约10个具有相同列名但行数不同的数据帧。 I would like to create a list of all columns with the same names. 我想创建一个具有相同名称的所有列的列表。

So, say i have 2 data frames with the same names. 所以,假设我有2个具有相同名称的数据帧。

a<-seq(0,20,1)
b<-seq(20,40,1)
c<-seq(10,30,1)

df.abc.1<-data.frame(a,b,c)

a<-seq(20,50,1)
b<-seq(10,40,1)
c<-seq(30,60,1)

df.abc.2<-data.frame(a,b,c)

I know i can create a list from this data such as, 我知道我可以根据这些数据创建一个列表,例如

list(df.abc.1$a, df.abc.2$a)

but i don't want to type out my long data frame names and column names. 但我不想输入我的长数据框名称和列名。

I was hoping to do something like this, 我希望做这样的事情,

list(c(df.abc.1, df.abc.2)$a)

But, it returns a list of df.abc.1$a 但是,它返回一个df.abc.1 $ a的列表

Perhaps there could be a way to use the grep function across multiple data.frames? 也许有可能在多个data.frames中使用grep函数? Perhaps a loop could accomplish this task? 也许一个循环可以完成这个任务?

Not sure if it's any better, but maybe 不确定它是否更好,但也许

lapply(list(df.abc.1, df.abc.2), function(x) x$a)

For more than one column 对于多个专栏

lapply(list(df.abc.1, df.abc.2), function(x) x[, c("a","b")])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM