Documentation:Reading in Data in R

From UBC Wiki

It can sometimes be useful to read Data in from another file using R. There are a few different ways this can be accomplished using both Rstat and Rstudio. The three types of files that will be discussed are CSV files, txt files, and excel files.

In RStudio

RStat user interface

To upload data from a file into Rstudio, choose one of the two methods below.

  1. Open R. Under the Environment tab, select the option Import dataset.
  2. Click The option "From textfile...".
  3. Find the file you wish to upload.
  4. Select the options that are appropriate for your file, most importantly, the separator option and the header option.
  5. Click submit, and R will run code for you that will upload the data into the data section of R. R will save the data to an object named after the file
  6. If your data has multiple columns that you entered, you can select the column you wish to plot with the following code.

"name_of_data"[7] - if you wish to read the seventh column.


Alternatively, you can manually import a document using the following code.


>mydata= read.table("c:/mydata.csv", header=TRUE)
read.table will parse CSV and txt files, the first argument is the file name and the location of the file, This must include the directories. The second argument will tell R whether the file has a header or not.


You can import data from an excel file by using the following code

>library("xlsx")
>read.xlsx("c:/mydata.xlsx", sheetname = "mysheet")
Akin to parsing from a txt file and a CSV file, read.xlsx's first argument takes the path to the file. It's second argument will take the name of the sheet you wish to import.

Note: library("xlsx") requires another library called rJava to run, if you are operating on a 64-bit windows OS, R and your Java program may not have the same architecture, to ensure that this library will work, download Java 64-bit to your computer.

If you plan to extract multiple files from the same working directory, it may be beneficial to set the location of those files as your working directory(eg. c:/users/documents as the working directory for a file and path c:/user/documents/myfile.txt)

To do this, use the following code
>setwd("C:/Documents and Settings/username/My Documents/x/y/z")
Where the input value is the directory you wish to work from. This command will work in Rstudio, or RStat
Note: R will not be able to read files located outside your working directory.

Alternatively, you can set your working directory by clicking the session tab in the top menu->Set Working Directory->Choose Directory, and then selecting the directory you wish to work from.

In RStat

If you plan to use Rstat instead of Rstudio, you must manually script R to import data from a file. To script txt and CSV files use the following code.

>mydata=read.table("myfile.csv", header=TRUE)

-The first argument is the directory and name of the file, the second argument tells R is there is a header or not.

If you plan to extract from a excel file, you must do the following. install the xlsx package(if you have not already done so) using the command
>install.packages("xlsx")
import the library using the command
>library("xlsx")
You can now read the file using the command
>mydata=read.xlsx("myfile.xlsx", heading=TRUE) The inputs are akin to the inputs above.

RStat user interface

Note: To install packages in R, you need to have Administrator access on the computer. To check data that has already been parsed into R type the name of the variable that you saved the data to (eg. type mydata to view the data) or if you want to view a single column of the data, type the variable name, followed by [#], where # is the number of the column you wish to view.


Note:If operating on a mac, the default directory will be displayed as /user/username,. If you are operating on a windows the default directory will be displayed as C:/users/username/Documents.