## 9.6 .txt files

While .RData files are great for saving R objects, sometimes you’ll want to export data (usually dataframes) as a simple .txt text file that other programs, like Excel and Shitty Piece of Shitty Shit, can also read. To do this, use the write.table() function.

### 9.6.1 write.table()

Table 9.2: Arguments for the write.table() function that will save an object x (usually a data frame) as a .txt file.
Argument Description
x The object you want to write to a text file, usually a dataframe
file The document’s file path relative to the working directory unless specified otherwise. For example file = "mydata.txt" saves the text file directly in the working directory, while file = "data/mydata.txt" will save the data in an existing folder called data inside the working directory.
You can also specify a full file path outside of your working directory (file = "/Users/CaptainJack/Desktop/OctoberStudy/mydata.txt")
sep A string indicating how the columns are separated. For comma separated files, use sep = ",", for tab–delimited files, use sep = "\t"
row.names A logical value (TRUE or FALSE) indicating whether or not save the rownames in the text file. (row.names = FALSE will not include row names)

For example, the following code will save the pirates dataframe as a tab–delimited text file called pirates.txt in my working directory:

# Write the pirates dataframe object to a tab-delimited
#  text file called pirates.txt in my working directory

write.table(x = pirates,
file = "pirates.txt",  # Save the file as pirates.txt
sep = "\t")            # Make the columns tab-delimited

If you want to save a file to a location outside of your working directory, just use the entire directory name. When you enter a long path name into the file argument of write.table(), R will look for that directory outside of your working directory. For example, to save a text file to my Desktop (which is outside of my working directory), I would set file = "Users/nphillips/Desktop/pirates.txt".

# Write the pirates dataframe object to a tab-delimited
#  text file called pirates.txt to my desktop

write.table(x = pirates,
file = "Users/nphillips/Desktop/pirates.txt",  # Save the file as pirates.txt to my desktop
sep = "\t")                                    # Make the columns tab-delimited

If you have a .txt file that you want to read into R, use the read.table() function.

Argument Description
file The document’s file path relative to the working directory unless specified otherwise. For example file = "mydata.txt" looks for the text file directly in the working directory, while file = "data/mydata.txt" will look for the file in an existing folder called data inside the working directory.
If the file is outside of your working directory, you can also specify a full file path (file = "/Users/CaptainJack/Desktop/OctoberStudy/mydata.txt")
header A logical value indicating whether the data has a header row – that is, whether the first row of the data represents the column names.
sep A string indicating how the columns are separated. For comma separated files, use sep = ",", for tab–delimited files, use sep = "\t"
stringsAsFactors A logical value indicating whether or not to convert strings to factors. I always set this to FALSE because I hate, hate, hate how R uses factors

The three critical arguments to read.table() are file, sep, header and stringsAsFactors. The file argument is a character value telling R where to find the file. If the file is in a folder in your working directory, just specify the path within your working directory (e.g.; file = data/newdata.txt. The sep argument tells R how the columns are separated in the file (again, for a comma–separated file, use sep = ","}, for a tab–delimited file, use sep = "\t". The header argument is a logical value (TRUE or FALSE) telling R whether or not the first row in the data is the name of the data columns. Finally, the stringsAsFactors argument is a logical value indicating whether or not to convert strings to factors (I always set this to FALSE!)

Let’s test this function out by reading in a text file titled mydata.txt. Since the text file is located a folder called data in my working directory, I’ll use the file path file = "data/mydata.txt" and since the file is tab–delimited, I’ll use the argument sep = "\t":

# Read a tab-delimited text file called mydata.txt
#  from the data folder in my working directory into
#  R and store as a new object called mydata

mydata <- read.table(file = 'data/mydata.txt',    # file is in a data folder in my working directory
sep = '\t',                  # file is tab--delimited
header = TRUE,               # the first row of the data is a header row
stringsAsFactors = FALSE)    # do NOT convert strings to factors!!

### 9.6.3 Reading files directly from a web URL

A really neat feature of the read.table() function is that you can use it to load text files directly from the web (assuming you are online). To do this, just set the file path to the document’s web URL (beginning with http://). For example, I have a text file stored at http://goo.gl/jTNf6P. You can import and save this tab–delimited text file as a new object called fromweb as follows:

# Read a text file from the web
sep = '\t',

# Print the result
fromweb
##            message randomdata
## 1 Congratulations!          1
## 2              you          2
## 3             just          3
## 5             this          5
## 6            table          6
## 7             from          7
## 8              the          8
## 9             web!          9

I think this is pretty darn cool. This means you can save your main data files on Dropbox or a web-server, and always have access to it from any computer by accessing it from its web URL.

#### Debugging

When you run read.table(), you might receive an error like this:

Error in file(file, “rt”) : cannot open the connection
If you receive this error, it’s likely because you either spelled the file name incorrectly, or did not specify the correct directory location in the file argument.