简体   繁体   English

从多个数据框的列表中提取特定的列,并将它们组合成r中的新数据框

[英]extracting particular column from a list of multiple data frames and combine them into a new data frame in r

How do I extract specific column of multiple dataframs of a list for example the 8th column of all dfs and combine the extracted columns into a new data frame. 如何提取列表中多个数据帧的特定列,例如所有dfs的第8列,并将提取的列合并为一个新的数据帧。

I am using a for loop that does not give me the desired output. 我使用的是for循环,无法提供所需的输出。 I prefer to use lapply() function instead of for loops. 我更喜欢使用lapply()函数而不是for循环。 Do you have any idea how can I do this? 你知道我该怎么做吗?

new_df <- data.frame()

for(i in 1:length(list_of_dfs)){

    col_8 <- list_of_dfs[[i]][8]

    new.df[i] <- col_8
}

View(df)

You can use lapply and do.call to achieve that : 您可以使用lapplydo.call来实现:

library(magrittr)
lapply(list_of_dfs,function(i) i[,8]) %>% do.call(cbind,.) %>% data.frame

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

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