Reputation: 395
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.
I am using a for loop that does not give me the desired output. I prefer to use lapply() function instead of for loops. 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)
Upvotes: 1
Views: 1138
Reputation: 3749
You can use lapply
and do.call
to achieve that :
library(magrittr)
lapply(list_of_dfs,function(i) i[,8]) %>% do.call(cbind,.) %>% data.frame
Upvotes: 2