We can use R in isolation…
For this, we need files.
What is a file?
For our purposes, just think of a file as a blob of saved text.
Suppose we wanted to represent a complex structure in a file.
To do this, we need to agree on some convention (format) of representing the structure in plain text.
What if we wanted to represent a table?
In markdown:
| Some Column | Another Column | Yet Another |
|-------------|----------------|-------------|
| 3 | "hello" | TRUE |
| 17 | "goodbye" | FALSE |
Any issues with this?
One common format for representing tables is .csv
– “comma separated values”.
name,position,student
Gaston,Professor,FALSE
Jakob,uGSI,TRUE
Joy,Tutor,TRUE
Each row is represented by a line, with the values in each column separated by commas.
The first row optionally includes the column names.
The general function for importing tables from files is read.table()
.
read.table(file, header = FALSE, sep = "", quote = "\"'",
dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"),
row.names, col.names, as.is = !stringsAsFactors,
na.strings = "NA", colClasses = NA, nrows = -1,
skip = 0, check.names = TRUE, fill = !blank.lines.skip,
strip.white = FALSE, blank.lines.skip = TRUE,
comment.char = "#",
allowEscapes = FALSE, flush = FALSE,
stringsAsFactors = FALSE,
fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)
file
: The file to read fromheader
: Does the file have a header for the column names?sep
: What separates the data (e.g., ','
for .csv
files)?To avoid writing passing read.table()
arguments to correctly parse .csv
files, we have a convenience function:
read.csv(file, header = TRUE, sep = ",", quote = "\"",
dec = ".", fill = TRUE, comment.char = "", ...)
And for other common formats:
read.csv2(file, header = TRUE, sep = ";", quote = "\"",
dec = ",", fill = TRUE, comment.char = "", ...)
read.delim(file, header = TRUE, sep = "\t", quote = "\"",
dec = ".", fill = TRUE, comment.char = "", ...)
read.delim2(file, header = TRUE, sep = "\t", quote = "\"",
dec = ",", fill = TRUE, comment.char = "", ...)
read.table()
Suppose we have a file starwars.csv
including the following contents.
"name","height","mass"
"Luke Skywalker",172,77
"C-3PO",167,75
"R2-D2",96,32
"Darth Vader",202,136
...
read.table()
SolutionSuppose we have a file starwars.csv
including the following contents.
"name","height","mass"
"Luke Skywalker",172,77
"C-3PO",167,75
"R2-D2",96,32
"Darth Vader",202,136
...
What if our values were separated with ';'
instead?
We have write.table()
for writing tables to files.
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",
eol = "\n", na = "NA", dec = ".", row.names = TRUE,
col.names = TRUE, qmethod = c("escape", "double"),
fileEncoding = "")
And for convenience,
write.csv(...)
write.csv2(...)
If we want to save the output of our program to a file, we use sink()
.
The output of our program was not printed to the console, but redirected to the file my-output.txt
, which now contains: