0

I have an Excel file with 4 tabs, I read these tabs using the following codes:

   

 library(rio)
 library(tidyverse)
  list<- import_list("test.xlsx", setclass = "tbl")

I get 4 Tibbles for each tab in Excel. I want to stack these Tibbles to get one file. I don't know how to uses stack().

Here is a small sample of the data

Sheet 1
ID  colour  size
12  Yellow  2
11  Red 3

Sheet 2
ID  colour  size
11  Blue    1
9   Red 1

sheet3
ID  colour  size
7   black   1
9   yellow  2

sheet 4
ID  colour  size
2   Red 1
4   Yellow  1

and the outcome is

ID  colour  size
12  Yellow  2
11  Red 3
11  Blue    1
9   Red 1
7   black   1
9   yellow  2
2   Red 1
4   Yellow  1
user330
  • 1,256
  • 1
  • 7
  • 12
  • The verb "stack" could mean several things in the context of dataframes and such, many of them having nothing to do with the function `utils::stack()`. Please use `dput` to provide the the two rows (and limit the columns if they are not necessary) of your four tibbles, and then please show what you expect the output to be. – r2evans May 06 '21 at 13:15

1 Answers1

0

Try the below.

BTW, it's generally bad practice to name an object the same as a base R function, especially something as heavily-used as list. I'm using L here to indicate the object that contains your four frames.

do.call(rbind, L)
#   ID colour size
# 1 12 Yellow    2
# 2 11    Red    3
# 3 11   Blue    1
# 4  9    Red    1
# 5  7  black    1
# 6  9 yellow    2
# 7  2    Red    1
# 8  4 Yellow    1

### equivalent, if you're already using `dplyr` or `data.table`
dplyr::bind_rows(L)
data.table::rbindlist(L)

Data as provided by the output of dput(L).

L <- list(structure(list(ID = 12:11, colour = c("Yellow", "Red"), size = 2:3), class = "data.frame", row.names = c(NA, -2L)), structure(list(ID = c(11L, 9L), colour = c("Blue", "Red"), size = c(1L, 1L)), class = "data.frame", row.names = c(NA, -2L)), structure(list(ID = c(7L, 9L), colour = c("black", "yellow"), size = 1:2), class = "data.frame", row.names = c(NA, -2L)), structure(list(ID = c(2L, 4L), colour = c("Red", "Yellow" ), size = c(1L, 1L)), class = "data.frame", row.names = c(NA, -2L)))
r2evans
  • 141,215
  • 6
  • 77
  • 149