Excel 통합 문서의 모든 워크 시트를 data.frames가있는 R 목록으로 읽습니다.
XLConnect
Excel 워크 시트를 R로 읽는 데 사용할 수 있음을 이해합니다 . 예를 들어, 이것은 test.xls
R로 호출되는 통합 문서의 첫 번째 워크 시트를 읽 습니다.
library(XLConnect)
readWorksheetFromFile('test.xls', sheet = 1)
여러 워크 시트가있는 Excel 통합 문서가 있습니다.
통합 문서의 모든 워크 시트를 R의 목록으로 가져올 수 있습니다. 여기서 목록의 각 요소는 주어진 시트에 대한 data.frame이고 각 요소의 이름은 Excel의 워크 시트 이름과 일치합니까?
readxl을 사용하여 답변 업데이트 (2015 년 6 월 22 일)
이 질문을 게시 한 이후 readxl
패키지가 출시되었습니다. xls
및 xlsx
형식을 모두 지원 합니다. 중요한 것은 다른 Excel 가져 오기 패키지와 달리 추가 소프트웨어를 설치하지 않고도 Windows, Mac 및 Linux에서 작동합니다.
따라서 Excel 통합 문서의 모든 시트를 가져 오는 기능은 다음과 같습니다.
library(readxl)
read_excel_allsheets <- function(filename, tibble = FALSE) {
# I prefer straight data.frames
# but if you like tidyverse tibbles (the default with read_excel)
# then just pass tibble = TRUE
sheets <- readxl::excel_sheets(filename)
x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X))
if(!tibble) x <- lapply(x, as.data.frame)
names(x) <- sheets
x
}
다음과 같이 호출 할 수 있습니다.
mysheets <- read_excel_allsheets("foo.xls")
이전 답변
@mnel에서 제공하는 답변을 기반으로 Excel 파일을 인수로 사용하고 각 시트를 명명 된 목록의 data.frame으로 반환하는 간단한 함수가 있습니다.
library(XLConnect)
importWorksheets <- function(filename) {
# filename: name of Excel file
workbook <- loadWorkbook(filename)
sheet_names <- getSheets(workbook)
names(sheet_names) <- sheet_names
sheet_list <- lapply(sheet_names, function(.sheet){
readWorksheet(object=workbook, .sheet)})
}
따라서 다음과 같이 호출 할 수 있습니다.
importWorksheets('test.xls')
대부분의 XLConnect 함수는 이미 벡터화되어 있습니다. 즉, 명시 적 벡터화를 수행하지 않고도 한 번의 함수 호출로 모든 워크 시트를 읽을 수 있습니다.
require(XLConnect)
wb <- loadWorkbook(system.file("demoFiles/mtcars.xlsx", package = "XLConnect"))
lst = readWorksheet(wb, sheet = getSheets(wb))
XLConnect 0.2-0 lst 는 이미 명명 된 목록이됩니다.
공식 readxl
(tidyverse) 문서에서 (첫 번째 줄 변경) :
path <- "data/datasets.xlsx"
path %>%
excel_sheets() %>%
set_names() %>%
map(read_excel, path = path)
I stumbled across this old question and I think the easiest approach is still missing.
You can use rio
to import all excel sheets with just one line of code.
library(rio)
data_list <- import_list("test.xls")
If you're a fan of the tidyverse
, you can easily import them as tibbles by adding the setclass
argument to the function call.
data_list <- import_list("test.xls", setclass = "tbl")
Suppose they have the same format, you could easily row bind them by setting the rbind
argument to TRUE
.
data_list <- import_list("test.xls", setclass = "tbl", rbind = TRUE)
Since this is the number one hit to the question: Read multi sheet excel to list:
here is the openxlsx
solution:
filename <-"myFilePath"
sheets <- openxlsx::getSheetNames(filename)
SheetList <- lapply(sheets,openxlsx::read.xlsx,xlsxFile=filename)
names(SheetList) <- sheets
You can load the work book and then use lapply
, getSheets
and readWorksheet
and do something like this.
wb.mtcars <- loadWorkbook(system.file("demoFiles/mtcars.xlsx",
package = "XLConnect"))
sheet_names <- getSheets(wb.mtcars)
names(sheet_names) <- sheet_names
sheet_list <- lapply(sheet_names, function(.sheet){
readWorksheet(object=wb.mtcars, .sheet)})
excel.link will do the job.
I actually found it easier to use compared to XLConnect (not that either package is that difficult to use). Learning curve for both was about 5 minutes.
As an aside, you can easily find all R packages that mention the word "Excel" by browsing to http://cran.r-project.org/web/packages/available_packages_by_name.html
To read multiple sheets from a workbook, use readxl package as follows:
library(readxl)
library(dplyr)
final_dataFrame <- bind_row(path_to_workbook %>%
excel_sheets() %>%
set_names() %>%
map(read_excel, path = path_to_workbook))
Here, bind_row (dplyr) will put all data rows from all sheets into one data frame, and path_to_workbook is "dir/of/the/data/workbook".
I tried the above and had issues with the amount of data that my 20MB Excel I needed to convert consisted of; therefore the above did not work for me.
After more research I stumbled upon openxlsx and this one finally did the trick (and fast) Importing a big xlsx file into R?
https://cran.r-project.org/web/packages/openxlsx/openxlsx.pdf
Adding to Paul's answer. The sheets can also be concatenated using something like this:
data = path %>%
excel_sheets() %>%
set_names() %>%
map_df(~ read_excel(path = path, sheet = .x), .id = "Sheet")
Libraries needed:
if(!require(pacman))install.packages("pacman")
pacman::p_load("tidyverse","readxl","purrr")
'development' 카테고리의 다른 글
동시 HttpWebRequest의 최대 수 (0) | 2020.11.30 |
---|---|
친구 함수와 정적 멤버 함수를 어디에서 사용 하시겠습니까? (0) | 2020.11.30 |
DrawerLayout에서 제스처 리스너 비활성화 (0) | 2020.11.29 |
별도의 파일에있는 C # 클래스? (0) | 2020.11.29 |
명령 줄에서 스크립트를 실행하는 '보안 경고'무시 (0) | 2020.11.29 |