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()
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?
)
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.
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
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()
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")