How to Read Numeric Columns with read.xlsx2 in R
I like the xlsx package in R for its simplicity. However, I would always have a problem reading columns as numeric with the read.xlsx2
function. read.xlsx
reads numeric columns properly, but it's slow for large data sets.
By default, read.xlsx2
doesn't guess the type of each column so it just reads everything as strings. It's not immediately obvious from the documentation how to fix this without a bit of extra digging.
You actually have to look into the documentation of readColumns
to see that if you pass colClasses=NA
then it will guess the column type
So, use this:
data = read.xlsx2("path_to_xlsx_file.xlsx", sheetIndex=1, colClasses=NA)
1 comment
Thanks for the tip! Saved me twenty minutes of typing out data classes for each column :).
Comments are closed.