Read an Excel file directly from a R script

后端 未结 12 918
误落风尘
误落风尘 2020-11-22 13:52

How can I read an Excel file directly into R? Or should I first export the data to a text- or CSV file and import that file into R?

12条回答
  •  被撕碎了的回忆
    2020-11-22 14:35

    Another solution is the xlsReadWrite package, which doesn't require additional installs but does require you download the additional shlib before you use it the first time by :

    require(xlsReadWrite)
    xls.getshlib()
    

    Forgetting this can cause utter frustration. Been there and all that...

    On a sidenote : You might want to consider converting to a text-based format (eg csv) and read in from there. This for a number of reasons :

    • whatever your solution (RODBC, gdata, xlsReadWrite) some strange things can happen when your data gets converted. Especially dates can be rather cumbersome. The HFWutils package has some tools to deal with EXCEL dates (per @Ben Bolker's comment).

    • if you have large sheets, reading in text files is faster than reading in from EXCEL.

    • for .xls and .xlsx files, different solutions might be necessary. EG the xlsReadWrite package currently does not support .xlsx AFAIK. gdata requires you to install additional perl libraries for .xlsx support. xlsx package can handle extensions of the same name.

提交回复
热议问题