Search code examples
rxlsxreadxl

Skip rows while use read_excel or read.excel in R


I have a excel file like this:

enter image description here

I try to read it in read.xlsx or read_excel by skipping the second row:

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

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

The first one (read.xlsx), I didn't find parameters for skip rows, the second one give a df without headers.

Where did I do wrong in the code above and how to read it correctly? Thanks.


Solution

  • Read it twice: once for column names, then for the data:

    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
    

    Example input: test123.xlsx

    enter image description here