【发布时间】:2018-08-14 02:25:05
【问题描述】:
我正在使用 readxl 库读取同一个 Excel 工作簿(称为 data.xlsx)中的多个 Excel 工作表,格式如下:
数据从第 3 行开始。
row1
row2
companyName 1980 1981 1982 ... 2016
company1 5 6 7 8
company2 10 20 30 40
company3 20 40 60 80
....
数据范围的长度因行和列而异。但是,它们将 companyName 作为通用键。 年份范围从 1980 年或 1990 年到 2016 年不等。工作表名称是数据名称。
我想创建一个从所有工作表中提取所有数据的 Excel。
companyName Year dataname values
company1 1980 sheetname1 5
company1 1981 sheetname1 6
company1 1982 sheetname1 7
company1 ... sheetname1 ...
company1 2016 sheetname1 8
company2 1980 sheetname1 10
company2 1981 sheetname1 20
company2 1982 sheetname1 30
company2 ... sheetname1 ...
company2 2016 sheetname1 40
.... .... ... ...
company1 2000 sheetname2 xxx
company1 2001 sheetname2 yyy
etc
etc
etc
这也是我设法达到的程度:
library(tidyverse)
library(readxl)
library(data.table)
#read excel file (from [here][1])
file.list<-"data.xlsx"
**#read all sheets (and **skip** first two rows)**
df.list <- lapply(file.list,function(x) {
sheets <- excel_sheets(x)
dfs <- lapply(sheets, function(y) {
read_excel(x, sheet = y,skip=2)
})
names(dfs) <- sheets
dfs
})
我有以下问题:
- 前两行没有被跳过
- 如何创建一个仅包含选定工作表的数据框(即工作表 5、工作表 10 和工作表 15)。
感谢您的帮助。
来源: R: reading multiple excel files, extract first sheet names, and create new column
【问题讨论】:
-
您的
readxl软件包是什么版本?我没有跳过行的问题。除非文件中的所有工作表都以标题前相同的行数开头。 -
您好 - 使用 readxl 的 1.0.0 包。是的,需要排除一些工作表,请问我该怎么做?
-
你的意思是
skips 不同吗?
标签: r