简体   繁体   中英

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)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM