Create a dataframe with list elements with dplyr in R

Viewed 2690

This is my dataframe:

    df<-list(structure(list(Col1 = structure(1:6, .Label = c("A", "B", 
"C", "D", "E", "F"), class = "factor"), Col2 = structure(c(1L, 
2L, 3L, 2L, 4L, 5L), .Label = c("B", "C", "D", "F", "G"), class = "factor")), class = "data.frame", row.names = c(NA, 
-6L)), structure(list(Col1 = structure(c(1L, 4L, 5L, 6L, 2L, 
3L), .Label = c("A", "E", "H", "M", "N", "P"), class = "factor"), 
    Col2 = structure(c(1L, 2L, 3L, 2L, 4L, 5L), .Label = c("B", 
    "C", "D", "F", "G"), class = "factor")), class = "data.frame", row.names = c(NA, 
-6L)), structure(list(Col1 = structure(c(1L, 4L, 6L, 5L, 2L, 
3L), .Label = c("A", "W", "H", "M", "T", "U"), class = "factor"), 
    Col2 = structure(c(1L, 2L, 3L, 2L, 4L, 5L), .Label = c("B", 
    "C", "D", "S", "G"), class = "factor")), class = "data.frame", row.names = c(NA, 
-6L))) 

I want to extract col1=df[[1]][1] as a dataframe. Then col1 of the second position of this list I want to merge to the df[[1]][1], then I will have a dataframe with 2 columns. After this I want to merge the column 1 of the third position of the list to the dataframe with two columns, then I will have a dataframe with 3 columns.

In other words my dataframe should have 3 columns, all the first columns of each entry of my list.

The dplyr package can helpme to do this?

Any help?

5 Answers

You can use lapply to extract the three columns named "Col1 in one go. Then set the names of the result.

col1 <- as.data.frame(lapply(df, '[[', "Col1"))
names(col1) <- letters[seq_along(col1)]

col1
#  a b c
#1 A A A
#2 B M M
#3 C N U
#4 D P T
#5 E E W
#6 F H H

Choose any other column names that you might find better.

A dplyr way could be

df %>% 
  unlist(recursive = FALSE) %>%
  as.data.frame %>%
  select(., starts_with("Col1"))
#  Col1 Col1.1 Col1.2
#1    A      A      A
#2    B      M      M
#3    C      N      U
#4    D      P      T
#5    E      E      W
#6    F      H      H

With map_dfc from purrr:

library(purrr)

map_dfc(df, `[`, 1)

Output:

  Col1 Col11 Col12
1    A     A     A
2    B     M     M
3    C     N     U
4    D     P     T
5    E     E     W
6    F     H     H

Alternative use of map_dfc making use of purrr's concise element extraction syntax that allows specifying elements of elements by name or position. The first is, for example, equivalent to

map_dfc(df, `[[`, 1)

which differs from the use of [ in that the columns will not be named variations of Col1 and just get V names instead, which may be desirable since names like Col11 and Col12 may be confusing.

df <- list(structure(list(Col1 = structure(1:6, .Label = c("A", "B", "C", "D", "E", "F"), class = "factor"), Col2 = structure(c(1L, 2L, 3L, 2L, 4L, 5L), .Label = c("B", "C", "D", "F", "G"), class = "factor")), class = "data.frame", row.names = c(NA, -6L)), structure(list(Col1 = structure(c(1L, 4L, 5L, 6L, 2L, 3L), .Label = c("A", "E", "H", "M", "N", "P"), class = "factor"), Col2 = structure(c(1L, 2L, 3L, 2L, 4L, 5L), .Label = c("B", "C", "D", "F", "G"), class = "factor")), class = "data.frame", row.names = c(NA, -6L)), structure(list(Col1 = structure(c(1L, 4L, 6L, 5L, 2L, 3L), .Label = c("A", "W", "H", "M", "T", "U"), class = "factor"), Col2 = structure(c(1L, 2L, 3L, 2L, 4L, 5L), .Label = c("B", "C", "D", "S", "G"), class = "factor")), class = "data.frame", row.names = c(NA, -6L)))

library(purrr)
map_dfc(df, 1)
#> # A tibble: 6 x 3
#>   V1    V2    V3   
#>   <fct> <fct> <fct>
#> 1 A     A     A    
#> 2 B     M     M    
#> 3 C     N     U    
#> 4 D     P     T    
#> 5 E     E     W    
#> 6 F     H     H
map_dfc(df, "Col1")
#> # A tibble: 6 x 3
#>   V1    V2    V3   
#>   <fct> <fct> <fct>
#> 1 A     A     A    
#> 2 B     M     M    
#> 3 C     N     U    
#> 4 D     P     T    
#> 5 E     E     W    
#> 6 F     H     H

Created on 2018-09-19 by the reprex package (v0.2.0).

res<-1:nrow(df[[1]][1])

for(i in 1:length(df)){
  print ( as.vector(df[[i]][1]))
  res<-cbind(res,as.data.frame(df[[i]][1]))
}
res$res<-NULL

So, the output is:

  Col1 Col1 Col1
1    A    A    A
2    B    M    M
3    C    N    U
4    D    P    T
5    E    E    W
6    F    H    H

Using dplyr

library(dplyr)
df %>% 
  sapply('[[',1) %>%
  as.data.frame
#returns
  V1 V2 V3
1  A  A  A
2  B  M  M
3  C  N  U
4  D  P  T
5  E  E  W
6  F  H  H
Related