read.xls-读取长度可变的工作表列表及其名称 [英] read.xls - read in variable-length list of sheets, with their names
问题描述
给出几个具有不同张数的.xls
文件,我正在使用gdata
包中的read.xls
将它们读入R
中.我有两个相关的问题(解决第二个问题应该解决第一个问题):
Given several .xls
files with varying number of sheets, I am reading them into R
usingread.xls
from the gdata
package. I have two related issues (solving the second issue should solve the first):
- 提前知道每个
.xls
文件将有多少张纸,实际上,此值在一个文件与另一个文件之间会有所不同. - 我需要捕获工作表的名称,这是相关数据
- It is unknown ahead of time how many sheets each
.xls
file will have, and in fact this value will vary from one file to the next. - I need to capture the name of the sheet, which is relevant data
现在,要解决(1),我正在使用try()
并遍历工作表编号,直到遇到错误.
Right now, to resolve (1), I am using try()
and iterating over sheet numbers until I hit an error.
如何获取工作表名称的列表,以便可以对其进行遍历?
How can I grab a list of the names of the sheet so that I can iterate over them?
推荐答案
请参见,则将电子表格的所有工作表读入列表,每个组件一个工作表,就是这样:
See the sheetCount and sheetNames
functions (on same help page) in gdata. If xls <- "a.xls"
, say, then reading all sheets of a spreadsheet into a list, one sheet per component, is just this:
sapply(sheetNames(xls), read.xls, xls = xls, simplify = FALSE)
请注意,将使用图纸名称来命名组件.根据内容,删除simplify = FALSE
可能很有意义.
Note that the components will be named using the names of the sheets. Depending on the content it might make sense to remove simplify = FALSE
.
这篇关于read.xls-读取长度可变的工作表列表及其名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!