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.
– r_rabbit
Yes. See the answer edition. In addition, if my answer helped you, consider voting for it and accepting it.
– Marcus Nunes
Thankfully, the creation of data.frames also worked by the command you edited.
– r_rabbit