使用read_excel或read.excel在r中跳过多个标头行
对于具有多行标头的Excel文件,如(从 there )):
我如何跳过行unit
和频率
并使用indodator_name
用r读取Excel时,作为Excel文件的标题?
使用下面的代码,似乎我只能通过将Skip参数设置为整数来跳过一行。
library(readxl)
myCols <- as.character(read_excel("./test123.xlsx", n_max = 1, col_names = FALSE))
myDF <- read_excel("./test123.xlsx", skip = 2, col_names = myCols)
参考:
For an excel file with multiple row of headers as such (test data for downloading from here):
How could I skip rows Unit
and Frequency
and use indicator_name
as header of excel file while reading excel with R?
With code below, it seems I could only skip one row by setting skip parameter an integer.
library(readxl)
myCols <- as.character(read_excel("./test123.xlsx", n_max = 1, col_names = FALSE))
myDF <- read_excel("./test123.xlsx", skip = 2, col_names = myCols)
Reference:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要
skip = 3
而不是 2,因为您在读入数据时需要跳过标头。由于我们已经在myCols
中定义了列名,因此当您读入列名时,我们不需要保留列名行。输出
如果第一个列名为空,那么您可以在读入数据之前替换列名称中的
NA
。基准测试
此时此刻,在读入行后过滤掉行似乎仍然会更快。
You just need to
skip = 3
instead of 2, as you need to skip the header when you read in the data. Since we have already defined column names inmyCols
, then we do not need to keep the column name row when you read it in.Output
If you have the first column name blank, then you could replace the
NA
in the column names before reading in the data.Benchmark
In this instant, it looks like it would still be faster to just filter out the rows after reading them in.