【问题标题】:Is it possible to delete the first few row of xlsx files (over 100 files) with multiple sheets in r?是否可以在 r 中删除多张 xlsx 文件(超过 100 个文件)的前几行?
【发布时间】:2023-02-24 16:42:02
【问题描述】:

我有一系列包含多个工作表的 xlsx 文件(每个文件 > 200mb)。只有第一张文件包含介绍,例如:

This table is designed for balabala etc... balabala
Reference Key date
1 01/01/1999

每个文件的介绍行数不一样,但所有数据集都以Reference Key变量开头。

将来自同一文件的工作表合并到一个 xlsx 文件中时,是否可以避免读取整个数据集并删除介绍?

【问题讨论】:

  • readxl::read_excel 中有跳过选项
  • 但是readxl::read_excel 中的跳过选项也会跳过每张纸中的n行,这不是我想要的
  • 此外,readxl::read_excel 的速度对我来说仍然太慢,尤其是与 fread 相比...
  • fread中的skip="string"选项呢?
  • fread不支持读取xlsx格式。

标签: r excel data.table tidyverse apache-arrow


【解决方案1】:

扩展我上面的评论。未经测试的代码,因为您没有给我们一个可重现的例子。

library(readxl)
library(tidyverse)

# Make the obvious edit here
myFiles <- list.files(path="<your path>", pattern="xlsx")

# Read one file
readFile <- function(f) {
  sheets <- excel_sheets(f)
  lapply(
    seq_along(sheets),
    function(x) read_excel(f, sheet=x, skip=ifelse(x == 1, 1, 0))
  ) %>% 
  # Combine all sheets in the file into a single data frame
  bind_rows()
}

# Process your files
excelFiles <- lapply(myFiles, readFile)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    相关资源
    最近更新 更多