Bind rows by by select column names from a list?

  • A+
Category:Languages

I have a list of date frames:

x <- data.frame("SN" = 1:25,"Age" = 1:25,"Gender" = rep(c("Male","Female"),25)) y <- data.frame("SN" = 1:25,"Age" = 1:25,"Occupation" = rep(c("Barber","Doctor"),25)) z <- data.frame("SN" = 1:25,"Age" = 1:25) list <- c(x,y,z) 

I am attempting to bind the data frames together into one data frame, unlisted. I want to pull only specific columns to make the end data frame however.

So for instance, I want x,y, and z combined into one data frame with only SN and Age in the resulting data frame.

Is there a simple way of doing this?

 


An approach with purrr:

library(purrr) lst %>% map_dfr(`[`, c("SN", "Age")) 

Which says, map the extract [ function over the items "SN" and "Age" from each list, then bind together all those elements into a data.frame df -- and hey, while you're binding them together, please bind the rows so dfr.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: