0

I have an Excel file with several sheets, all containing the same data structure. I'm using readxl to read the sheets, and I store these results in a list, on sheet as one element. I'd now like to concatenate these into one dataframe. My structure after reading the data is the same as in the example list below (I've omitted the reading XLS part for clarity)

example_list = list(sheet1=tibble(A=c(1,2), B=c(3,4)), sheet2 = tibble(A=c  (5,6), B=c(7,8)))
example_list
 $sheet1
 # A tibble: 2 x 2
      A     B
   <dbl> <dbl>
1     1     3
2     2     4

$sheet2
# A tibble: 2 x 2
      A     B
  <dbl> <dbl>
1     5     7
2     6     8

My result should look like this:

# A tibble: 4 x 2
      A     B
  <dbl> <dbl>
1     1     3
2     2     4
3     5     7
4     6     8

I tried flatten(example_list) but this did not give me the desired result.

MichaelA
  • 1,866
  • 2
  • 23
  • 38

1 Answers1

2

flatten squashes the list elements to vector, we need bind_rows

library(dplyr)
bind_rows(example_list)

Or with rbind from base R (in case we don't need any library)

do.call(rbind, example_list)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    also `bind_rows` allows adding a column with the source of the data uising the `.id` argument – Kresten Apr 08 '19 at 12:39