Getting Excel Files into R
- The best way to read in Excel Files is to use the read_excel function that's in the readxl library.
- To download the readxl library, we first have to install it. We can do this by typing: install.packages("readxl")
- That will run some code in your R Console and will take a few seconds to install. You will need the internet for this since we are downloading a new library. Once it is done, the code will be installed to your computer.
- In order to make the functions within the readxl package available for use in this session, you will have to use the library function (with the package name not in quotes): library(readxl)
- Now, we have at our disposal a whole bunch of new functions specifically geared towards working with Excel. Keep the library() function in your script.
- Now that readxl is loaded, we can read data into R from an Excel file by typing: menu<-read_excel("http://joeystanley.com/downloads/menu.xlsx", sheet=2)
- You can optionally specify which sheet of the file to read in by adding sheet=2 within the parenthesis. Otherwise R will assume the first sheet within the Excel workbook.
- There is another way to read in data that does not involve typing long path names. You can use the file.choose command instead. When you do this, a window will open up and you will be able to find your file and click on it just like you were opening any other file. For MAC computers, type: menu<-file.choose(). For Windows/PC computers, type menu<-choose,file()