Read an Excel file directly from a R script

后端 未结 12 883
误落风尘
误落风尘 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:47

    Expanding on the answer provided by @Mikko you can use a neat trick to speed things up without having to "know" your column classes ahead of time. Simply use read.xlsx to grab a limited number of records to determine the classes and then followed it up with read.xlsx2

    Example

    # just the first 50 rows should do...
    df.temp <- read.xlsx("filename.xlsx", 1, startRow=1, endRow=50) 
    df.real <- read.xlsx2("filename.xlsx", 1, 
                          colClasses=as.vector(sapply(df.temp, mode)))
    

提交回复
热议问题