read.delim, write.table

The read.delim function is typically used to read in delimited text files, where data is organized in a data matrix with rows representing cases and columns representing variables. We can also write a matrix or data frame to a text file using the write.table function. Be sure to review the arguments of write.table carefully since the default settings clutter the text file (often unnecessarily).
read.delim(file, header=TRUE, sep="\t")
  • file – A file location.
  • header – Whether the first line describes the column names.
  • sep – The table delimiter, often times a tab (\t) or comma.
write.table(x, file="", …, quote=TRUE, sep=" ", row.names=TRUE)
  • x – A matrix or data frame to write to a file.
  • file – A file location.
  • quote – Whether characters or factors should have quotation marks written to the file.
  • sep – The table delimiter, often times a tab (\t) or comma.
  • row.names – Whether the row names of the matrix or data frame should be written as the first column in the file.

Example. A data frame is created with twelve rows and four variables: month, average temperature high, season, and whether school is in session during the majority of the corresponding month. The data frame is saved to a file called annual.txt, all objects are cleared from the workspace, and then the annual.txt data are pulled back into the R session.
> month    <- month.abb
> avgHigh  <- c(38, 41, 47, 56, 69, 81, 83, 82, 71, 55, 48, 43)
> seasons  <- c("Winter", "Spring", "Summer", "Fall")
> season   <- rep(seasons[c(1:4,1)], c(2,3,3,3,1))
> schoolIn <- rep(c("yes", "no", "yes"), c(5, 3, 4))
> 
> getwd()
[1] "/Users/ddiez/Dropbox/RFunction/code/1202"
> d        <- data.frame(month, avgHigh, season, schoolIn)
> write.table(d, "annual.txt", quote=FALSE,
+             sep="\t", row.names=FALSE)
> 
> rm(list=ls())
> ls()
character(0)
> d <- read.delim("annual.txt", header=TRUE, sep="\t")
> d
   month avgHigh season schoolIn
1    Jan      38 Winter      yes
2    Feb      41 Winter      yes
3    Mar      47 Spring      yes
4    Apr      56 Spring      yes
5    May      69 Spring      yes
6    Jun      81 Summer       no
7    Jul      83 Summer       no
8    Aug      82 Summer       no
9    Sep      71   Fall      yes
10   Oct      55   Fall      yes
11   Nov      48   Fall      yes
12   Dec      43 Winter      yes
The getwd function was used above to check the current working directory. This information was necessary to know where to find the text file on my computer later on.
Tip. There are several other related functions (e.g. read.csv) that may be reviewed by looking at the help files for read.delim and write.table.

Leave a Reply