Ghement Statistical Consulting Company Ltd.
2014
Web: [Link] E-mail: info@[Link] Phone: 604-767-1250
Reading .dat files in R
To read .dat files in R, you can use the function [Link]() which comes with the
default R installation.
The function [Link]() is related to other functions for reading data files into R,
such as [Link]() or [Link](). As a consequence, [Link]() has arguments
(or options) that are similar to the arguments of [Link]() or [Link]().
Some of the most commonly used options for [Link]() include:
header = TRUE (if the .dat file includes a first row which lists the variable
names)
sep = "" (if the data columns present in the .dat file are separated by
white spaces)
OR
sep = "," (if the data columns present in the .dat file are separated by
commas)
OR
sep = "/t" (if the data columns present in the .dat file are separated by
tabs)
skip = 2 (if the first 2 rows of the .dat file include comments and must be
ignored by R)
[Link] = TRUE (to prevent R from automatically converting character
variables, such as date variables or ID variables, into factors)
[Link] = "NA" (if missing values are coded as NA in the .dat file, where
For more details on other options for [Link](), please refer to Rs help file for
this function:
?[Link]
Example:
The example below illustrates the use of [Link]() to read a .dat file pulled from
the Internet.
filename <- "[Link]
data <- [Link](filename, header = TRUE, sep="", skip=2, [Link]=TRUE)
head(data)
If the .dat file in this example were stored in our R working directory, we would read
it in R and then view it using the commands below:
data <- [Link]("CZ03_2009.dat", header = TRUE, sep="", skip=2, [Link]=TRUE)
View(data)