1

I only want to import columns 1, 2, 4, 5 of my .xlsx file, so I tried:

read_xlsx(path, range=cell_cols(c(1, 2, 4, 5))

However, this imports all columns from column 1 to 5 (i.e. columns 1, 2, 3, 4, 5). Not what I want.

I cannot find the right way to do this using readxl. Am I missing something obvious?

francoiskroll
  • 1,026
  • 3
  • 13
  • 24
  • 2
    From the documentation for `cell_cols()`: Note it is not possible to request non-contiguous columns, i.e. columns 1, 2, and 5. In this case, the requested columns will run from the minimum of 1 to the maximum of 5. .... If you want to use `readxl()` you'll need to read the entire range in and subset after. – Ritchie Sacramento Feb 13 '23 at 11:10

1 Answers1

1

If you really want to skip a column, I suggest using the openxlsx::read.xlsx() for reading. It contains a rows and a cols argument, with whom you can control what rows/columns to read.

sample excel:

enter image description here

code:

library(openxlsx)
openxlsx::read.xlsx("./data/test_excel.xlsx", cols = c(1:3, 5:6))
#   a b c e f
# 1 1 2 3 5 6
Wimpel
  • 26,031
  • 1
  • 20
  • 37
  • Thanks. Unfortunately, openxlsx is slower and I have many files to import. Source: https://stackoverflow.com/questions/44538199/fast-way-to-read-xlsx-files-into-r – francoiskroll Feb 14 '23 at 17:08