I have a list composed of several dataframes, and I want to iterate over the list and pull the ‘nth’ column of each dataframe, and group all these elements side by side on a dataframe.
Consider that I need to pull the second column this list:
library(tidyverse)
mylist <- list(mt1 = mtcars, mt2 = mtcars*2, mt3 = mtcars*3)
I want a result similar to this, with cbind:
> mylist[[1]][2] %>%
+ cbind(mylist[[2]][2]) %>%
+ cbind(mylist[[3]][2]) %>%
+ head()
cyl cyl cyl
Mazda RX4 6 12 18
Mazda RX4 Wag 6 12 18
Datsun 710 4 8 12
Hornet 4 Drive 6 12 18
Hornet Sportabout 8 16 24
Valiant 6 12 18
But I need a code that can iterate over any number of list elements. That I would not need to rewrite depending on the number of list elements. How could I achieve it?
I can use a for loop, , but the output is different from what I need:
for (i in seq_along(mylist)){
print(mylist[[i]] %>% select(2))
}
The same with sapply or lapply:
sapply(mylist, function(x) x%>% select(2))
lapply(mylist, function(x) x%>% select(2))
With map_df I get a dataframe, but with each row on top of each other:
> map_df(mylist, function(x) x%>% select(2)) %>%
+ head()
cyl
Mazda RX4...1 6
Mazda RX4 Wag...2 6
Datsun 710...3 4
Hornet 4 Drive...4 6
Hornet Sportabout...5 8
Valiant...6 6
How can I pull the columns from each dataframe on the list, and arrange each column side by side?