【问题标题】:Skip rows while use read_excel or read.excel in R在 R 中使用 read_excel 或 read.excel 时跳过行
【发布时间】:2020-06-23 15:59:13
【问题描述】:

我有一个这样的 excel 文件:

我尝试通过跳过第二行以read.xlsxread_excel 阅读它:

library(xlsx)
df <- read.xlsx('./data.xls', 'Sheet1')

library(readxl)
df <- read_excel("./data.xls", sheet = 'Sheet0', skip = 2, col_names = TRUE)

第一个 (read.xlsx),我没有找到 skip 行的参数,第二个给出了一个没有标题的 df

我在上面的代码中哪里做错了,如何正确阅读?谢谢。

【问题讨论】:

  • 我不确定我们是否可以使用read_csv 读取.xlsx 文件,当我尝试这样做时会收到这样的错误:line 1 appears to contain embedded nullsline 2 appears to contain embedded nullsline 3 appears to contain embedded nullsline 4 appears to contain embedded nullsline 5 appears to contain embedded nullsError in make.names(col.names, unique = TRUE) :
  • 我明白了,你是对的。我认为下面zx8754的回答会有所帮助。我会按原样阅读excel,然后删除第二行。

标签: r xlsx readxl


【解决方案1】:

阅读两次:一次是列名,然后是数据:

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)

myDF
# # A tibble: 3 x 2
#   colAtitle colBtitle
#       <dbl>     <dbl>
# 1         1         5
# 2         2         6
# 3         3         7

示例输入:test123.xlsx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多