Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dataframe with list elements with dplyr in R

Tags:

r

dplyr

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?

like image 430
Laura Avatar asked Sep 19 '18 18:09

Laura


People also ask

Can you add a list to a DataFrame in R?

If a list has the same length of elements (not sub-elements) as the length of each vector for which we want to create the data frame then we first need to create the data frame of vectors then we can easily add the list into the data frame.

Does dplyr work with data frame?

All of the dplyr functions take a data frame (or tibble) as the first argument. Rather than forcing the user to either save intermediate objects or nest functions, dplyr provides the %>% operator from magrittr.


2 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
like image 167
Rui Barradas Avatar answered Nov 15 '22 01:11

Rui Barradas


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
like image 33
acylam Avatar answered Nov 15 '22 00:11

acylam