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)

One comment on “How to Read Numeric Columns with read.xlsx2 in R

  • 1

    Thanks for the tip! Saved me twenty minutes of typing out data classes for each column :).

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>