简体   繁体   中英

Import of several worksheets of an Excel workbook

I am relatively new to R and try to use some specific benefits of this program in my professional area. Now I need to load only several worksheets of a workbook into R and to apply a function to these several worksheets. I learned how to load and apply the function to all sheets. My script looks like:

wb <- loadWorkbook(filename = "book.xlsx")
lst <- readWorksheet(wb, sheet = getSheets(wb))
lapply(lst, function(w) { w <- summary(w[1:5]); w })

How can I modify this code in order to apply the function only to several worksheets, not to all?

Simply subset your list by indexed numbers or names in brackets. For instance, let's say the 1st, 3rd, 5th worksheets are the ones you need to run through function:

# FOR NAMED OR UNNAMED LIST
sublst <- lst[c(1,3,5)]

# FOR NAMED LIST
sublst <- lst[c("Sheet1", "Sheet3", "Sheet5")]

outputlst <- lapply(sublst,  function(w) ...)

Or specify the subsets of sheets during import:

sublst <- readWorksheet(wb, sheet = c("Sheet1", "Sheet3", "Sheet5"))

outputlst <- lapply(sublst,  function(w) ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM