TC Technology Knowledge Base

Get Data into R Excel Files in R-Studio

Updated on

The best way to read in Excel Files is to use the read_excel function that's in the readxl library.

1. In the Source Editor pane, type install.packages("readxl") and click Run to download the readxl library.

2. 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.

3. In order to make the functions in 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)

4. 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.

5. Now that readxl loaded, we can read data into R from an Excel file by typing: menu<-read_excel("http://joeystanley.com/downloads/menu.xlsx",sheet=2)

6. 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.

7. There another way to read in data that does not involve typing long path names. When you do this,  

7.1. Use the file.choose command instead.

7.2. 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.

7.3. For MAC computers, type: menu<-file.choose(), whereas for Windows/PC, type: menu<-choose,file()

Previous Article Import data into R- .txt files in R-Studio