使用 readxl 时不要跳过空的前导行

Don't skip empty leading rows when using readxl

Read_excel() 函数自动跳过前导空行。有没有办法避免这种情况,因为我需要 R 中的 row.names 与 excel 中的行号相对应?

当我不知道 read_excel 跳过了多少前导行时,无法满足此 objective。

您可以使用 read_excel 函数的 range 参数,因为该函数参数的文档说:

A cell range to read from, as described in cell-specification. Includes typical Excel ranges like "B3:D87", possibly including the sheet name like "Budget!B2:G14", and more. Interpreted strictly, even if the range forces the inclusion of leading or trailing empty rows or columns. Takes precedence over skip, n_max and sheet.

此外,您可能需要设置 col_names = FALSE,以使行编号保持不变。

如果您事先不知道哪些单元格可能是 missing/available,您可以使用:

readxl::read_excel(path, col_names = FALSE,
                   range = cellranger::cell_limits(ul =c(1L, 1L)))

这将读取所有单元格。它指定所需单元格范围的 upper-left ul 角并自动选择右下角。