Use bind_rows to convert list of vectors to dataframe

Viewed 434

I have a list of vectors that I would like to convert into a dataframe.

Code

a <- list( c(1,2,3,4), 
           c(1,2,3,4), 
           c(4,5,6,3), 
           c(6,3,2,6))

With help of this post, I was able to do so in the following manner:

library(tidyverse)
a %>% 
   reduce(rbind) %>% 
   as.data.frame()

> a %>% reduce(rbind) %>% as.data.frame()
      V1 V2 V3 V4
out    1  2  3  4
elt    1  2  3  4
elt.1  4  5  6  3
elt.2  6  3  2  6

I would like to use purrr's bind_rows() function (a %>% bind_rows), as it seems more convenient. However, this generates an error:

Error: Argument 1 must have names.

Questions

  1. What is happening here?
  2. How can I prevent it from happening ;) ?
1 Answers

One option could be:

map_dfr(a, ~ set_names(.x, paste0("V", seq_along(.x))))

     V1    V2    V3    V4
  <dbl> <dbl> <dbl> <dbl>
1     1     2     3     4
2     1     2     3     4
3     4     5     6     3
4     6     3     2     6
Related