How to Read Excel Files into R

Master R

By Guangming Lang Comment

You can use the XLConnect package to read .xls or .xlsx files into R. Suppose you have a file named example.xls in your working directory. You can read the data on sheet1 into R using the following commands.

data = readWorksheetFromFile("example.xls", sheet=1)

Alternatively, you can read in the data by sheet name. For example, the following code will read the data on the sheet named “raw_data” into R.

data = readWorksheetFromFile("example.xls", sheet="raw_data")

After reading an excel file into R, you often want to tidy up the data. Here’re some functions I use frequently for that purpose:

  • lapply
  • vapply
  • strsplit
  • paste
  • tolower
  • gsub

If you enjoyed this post, get updates. It's FREE