Lecture 5 Goals

  1. Learn to use read.table() to load data from a file
  2. Learn to use write.table() to write data to a file
  3. Use setwd() to set the working directory of your current R session

How do I get my data into R?

The cleanest way to load data into R is to restrict your datafiles to simple text files, where columns are either column-separated or tab-delimited. To do this, you can use the read.table() command. This command has 3 common arguments:

read.table()

  1. dir: The directory of the file on your computer. If you don’t know it, you need to find the file on your computer then look at it’s properties.
  2. header: A logical value indicating whether or not the data has a header row (indicating the column names).
  3. sep: An indicator of how the columns are separated. If they are separated by commas, use “,”. If they are separated by tabs, use “\t”.
  4. stringsAsFactors: A logical value indicating whether or not you want R to convert text colummns into factors. If you don’t know what a factor is, don’t worry, just set it to FALSE

When you are ready to load a datafile into R, you need to assign it to an object that you name. For example, let’s say I had a data file called “data.txt” on my desktop that is tab-delimited, and I want to assign it to a dataframe called “My.Data”. Here’s the code for loading the data into R

My.Data <- read.table(dir = "/Users/Nathaniel/Desktop/data.txt", # File directory
                      header = T, # Does the data have a header row?
                      sep = "\t", # How are columns separated?,
                      stringsAsFactors = F # Convert strings to factors?
                      )

But what about SPSS and Excel files?

One of the most common questions people ask when starting with R is “But how do I get my SPSS or Excel file into R?” Technically, you can load data from these and other common formats into R using the foreign package. However, I strongly recommend not using this (if for no other reason that you’ve already agreed to give up these software completely!). Instead, use SPSS (or Excel) to export the file to a tab-delimited text file format and then load the text file into R.

Using the setwd() function to make reading files easier

Let’s say you are reading multiple datafiles from an obscure folder on your computer. Instead of having to retype the file path every time you run read.table(), you can change the working directory in R using the setwd() function. When you set the working directory to a specific folder, then for all future commands, R will automatically look for files in that directory.

For example, let’s say you have 3 text files on your desktop, called “a.txt”, “b.txt”, “c.txt”. Instead of retyping the full directory each time, you can set the working directory to your desktop, then simply use the file names for future read.table() commands:

setwd(dir = "/Users/Nathaniel/Desktop") # Set the working directory to desktop

a <- read.table("a.txt", sep = "\t", header = T) # read first file
b <- read.table("b.txt", sep = "\t", header = T) # read second file
c <- read.table("c.txt", sep = "\t", header = T) # read third file

How do I write data from R?

To write data, specifically a matrix or a dataframe, from R, use the write.table() command. This command works very similarly to the read.table() command and has 3 common arguments:

write.table()

  1. x: The R object you want to write to a file. Typically a matrix or dataframe.
  2. file: The path that you want to save the file to and the name of the file you create.
  3. sep: How you want columns to be separated. Use “,” to separate by commans, and “” to separate by tabs:

For example, let’s say I have a dataframe in R called “Your.Data.Frame” that I want to save to a text file called “New_Data.txt”. I want to put the data file on my desktop and have the columns be tab-delimited:

write.table(x = Your.Data.Frame, # The object you want to write to a text file
            file = "/Users/Nathaniel/Desktop/New_Data.txt", # path and name
            sep = "\t" # How columns should be separated
            )

Alternatively, if I have already set the working directory to my desktop, I don’t need to re-type the full file path:

setwd(dir = "/Users/Nathaniel/Desktop"
write.table(Your.Data.Frame, file = "New_Data.txt", sep = "\t")