rexcelxlconnect

Maintain Original Column Names with XLConnect readWorksheet in R


I am reading in the column header from a .xlsx file using XLConnect::readWorksheet. The issue is that the column header contains / and spaces between words, which are replaced with . by the package when loading.

For example, an Excel spreadsheet with header First Name and Sex/Gender will appear in R as First.Name and Sex.Gender.

This is a problem, because I don't know if . represents a / or a space.

Is there a way to override this feature and simulate data.table functionality where column names are maintained and wrapped in a ` (backtick)?


Solution

  • There is an argument of check.names, with a default value of TRUE, if you change it to FALSE, then the column names will be maintained.

    XLConnect::readWorksheet(loadWorkbook("C:/Book1.xlsx"), sheet = 1, check.names = FALSE)
    

    You may also want to use the readxl packages as in the example:

    readxl::read_excel("C:/Book1.xlsx")
    

    This doesn't have an option for check.names or similar, but the default behavior does not change the column names.