9.4 .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.4.1 write.table()
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"
.
9.4.2 read.table()
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.4.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 https://raw.githubusercontent.com/ndphillips/ThePiratesGuideToR/refs/heads/master/data/BeardLengths.txt
. 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
fromweb <- read.table(file = 'https://raw.githubusercontent.com/ndphillips/ThePiratesGuideToR/refs/heads/master/data/BeardLengths.txt',
sep = '\t',
header = TRUE)
# Print the result
fromweb
## Ship Beard
## 1 Fearless Snake 17
## 2 Nervous Goat 41
## 3 Nervous Goat 0
## 4 Nervous Goat 41
## 5 Angry Badger 20
## 6 Angry Badger 32
## 7 Nervous Goat 0
## 8 Nervous Goat 39
## 9 Nervous Goat 1
## 10 Fearless Snake 22
## 11 Fearless Snake 20
## 12 Nervous Goat 0
## 13 Angry Badger 20
## 14 Fearless Snake 16
## 15 Fearless Snake 21
## 16 Nervous Goat 0
## 17 Fearless Snake 19
## 18 Nervous Goat 40
## 19 Angry Badger 27
## 20 Angry Badger 26
## 21 Angry Badger 16
## 22 Angry Badger 20
## 23 Angry Badger 22
## 24 Fearless Snake 15
## 25 Nervous Goat 1
## 26 Fearless Snake 19
## 27 Fearless Snake 21
## 28 Nervous Goat 41
## 29 Angry Badger 22
## 30 Nervous Goat 1
## 31 Angry Badger 22
## 32 Fearless Snake 23
## 33 Nervous Goat 41
## 34 Nervous Goat 1
## 35 Nervous Goat 1
## 36 Fearless Snake 16
## 37 Angry Badger 26
## 38 Angry Badger 26
## 39 Fearless Snake 17
## 40 Nervous Goat 41
## 41 Angry Badger 14
## 42 Angry Badger 22
## 43 Fearless Snake 21
## 44 Fearless Snake 14
## 45 Angry Badger 27
## 46 Nervous Goat 38
## 47 Nervous Goat 1
## 48 Fearless Snake 12
## 49 Nervous Goat 0
## 50 Nervous Goat 36
## 51 Fearless Snake 24
## 52 Angry Badger 21
## 53 Angry Badger 22
## 54 Fearless Snake 24
## 55 Angry Badger 27
## 56 Fearless Snake 21
## 57 Angry Badger 21
## 58 Fearless Snake 17
## 59 Nervous Goat 39
## 60 Fearless Snake 20
## 61 Nervous Goat 4
## 62 Nervous Goat 36
## 63 Angry Badger 21
## 64 Fearless Snake 20
## 65 Nervous Goat 34
## 66 Fearless Snake 14
## 67 Nervous Goat 39
## 68 Angry Badger 20
## 69 Nervous Goat 42
## 70 Nervous Goat 46
## 71 Angry Badger 17
## 72 Nervous Goat 36
## 73 Fearless Snake 18
## 74 Fearless Snake 20
## 75 Angry Badger 19
## 76 Nervous Goat 39
## 77 Nervous Goat 0
## 78 Nervous Goat 2
## 79 Fearless Snake 18
## 80 Angry Badger 28
## 81 Nervous Goat 39
## 82 Nervous Goat 42
## 83 Angry Badger 16
## 84 Angry Badger 20
## 85 Fearless Snake 16
## 86 Angry Badger 20
## 87 Fearless Snake 24
## 88 Angry Badger 27
## 89 Nervous Goat 3
## 90 Nervous Goat 1
## 91 Fearless Snake 18
## 92 Fearless Snake 18
## 93 Fearless Snake 21
## 94 Nervous Goat 0
## 95 Nervous Goat 2
## 96 Nervous Goat 39
## 97 Nervous Goat 32
## 98 Angry Badger 21
## 99 Fearless Snake 20
## 100 Angry Badger 15
## 101 Fearless Snake 18
## 102 Nervous Goat 0
## 103 Nervous Goat 38
## 104 Angry Badger 29
## 105 Nervous Goat 1
## 106 Angry Badger 23
## 107 Angry Badger 22
## 108 Angry Badger 24
## 109 Angry Badger 16
## 110 Fearless Snake 17
## 111 Nervous Goat 0
## 112 Nervous Goat 2
## 113 Fearless Snake 21
## 114 Fearless Snake 18
## 115 Fearless Snake 23
## 116 Fearless Snake 19
## 117 Nervous Goat 38
## 118 Fearless Snake 18
## 119 Angry Badger 22
## 120 Fearless Snake 16
## 121 Angry Badger 17
## 122 Angry Badger 27
## 123 Angry Badger 31
## 124 Fearless Snake 18
## 125 Angry Badger 26
## 126 Fearless Snake 20
## 127 Nervous Goat 3
## 128 Angry Badger 26
## 129 Angry Badger 14
## 130 Fearless Snake 17
## 131 Fearless Snake 24
## 132 Angry Badger 22
## 133 Fearless Snake 18
## 134 Fearless Snake 18
## 135 Fearless Snake 19
## 136 Fearless Snake 19
## 137 Nervous Goat 0
## 138 Angry Badger 22
## 139 Angry Badger 27
## 140 Angry Badger 26
## 141 Angry Badger 21
## 142 Nervous Goat 41
## 143 Nervous Goat 1
## 144 Angry Badger 26
## 145 Nervous Goat 40
## 146 Angry Badger 26
## 147 Fearless Snake 20
## 148 Fearless Snake 26
## 149 Fearless Snake 20
## 150 Angry Badger 26
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:
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.