Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't skip empty leading rows when using readxl

Tags:

r

readxl

Read_excel() function automatically skips leading empty rows. Is there a way to avoid this as I need the row.names in R to correspond with row numbers in excel?

It is impossible to satisfy this objective when I am unaware how many leading rows were skipped by read_excel.

like image 637
Eric Pios Avatar asked Oct 28 '25 01:10

Eric Pios


2 Answers

If you don't know in advance what cells might be missing/available, you can use:

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

This will read all cells. It specifies the upper-left ul corner of the desired cell range and automatically picks the bottom right.

like image 124
Michael Dewar Avatar answered Oct 30 '25 15:10

Michael Dewar


You can use the range argument of the read_excel function as the documentation for this argument of the function says:

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.

Additionally, you might want to set col_names = FALSE, such that the row-numbering stays the same.

like image 33
apitsch Avatar answered Oct 30 '25 16:10

apitsch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!