How to import multiple excel tabs at the same time?

Asked

Viewed 1,755 times

5

I am trying to reproduce the code below, but without success. What should I put in the argument path? Is it the same in all of them? Any clearer examples so I can understand? Any tips on how to import several "Sheets" excel at the same time?

# To load all sheets in a workbook:

path <- readxl_example("datasets.xls")
lapply(excel_sheets(path), read_excel, path = path)

###
In: https://cran.r-project.org/web/packages/readxl/readxl.pdf

1 answer

5


The first thing to do is install the package readxl, through the command

install.packages("readxl")

This step only needs to be done once. With the package readxl installed, it is necessary to load it into memory via command

library(readxl)

This step needs to be done every time R (or Rstudio) is closed and then reopened.

The argument path is the path to the file . xls on your computer. For example, when running path <- readxl_example("datasets.xls") on my PC, the result obtained is

path
[1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/readxl/
  extdata/datasets.xls"

The result on your computer will probably be different, but note that in the end, it will also have .xls, because this is the address of the sample file of the readxl. To read all sheets in a file . xls, run the commands

planilhas <- lapply(excel_sheets(path), read_excel, path = path)
length(planilhas)

The first line will read all spreadsheets inside a file . xls and save them inside an object called planilhas. The second command will tell you how many spreadsheets there were in this file. Now just run

planilhas[2]

for example, to see what data is present in sheet 2 of the file . xls read.


Editing after this comment:

It is possible to create a different data frame for each element in the list planilhas through the command

for (j in 1:length(planilhas)){
  assign(paste("planilha", j, sep=""), planilhas[[j]])
}
  • Marcos, thank you very much. It worked perfectly. I got a "Large List" with 11 elements. To convert each of these elements into data.frame I could use something like: spreadsheet4 <- spreadsheets[[4]]? I would do this for each element.

  • Yes. See the answer edition. In addition, if my answer helped you, consider voting for it and accepting it.

  • 1

    Thankfully, the creation of data.frames also worked by the command you edited.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.