Section 6 Read Data
6.1 Function
Function | Explanation |
---|---|
setwd |
Set the working directory. Note the file path is given by / or \\ . |
getwd |
Get the current working directory. Note the file path is given by / or \\ . |
read.table |
Read a tabular data - it reads a file in table format and creates a data frame from it, with lines and variables to fields in the file. It is always convenient to assign the outputs from read.table to an R object. |
read.csv |
Read a csv file. It is always convenient to assign the outputs from read.csv to an R object. |
readLines |
Read lines of a text file |
source |
Read the R script file |
6.2 Example
?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 = default.stringsAsFactors(),
fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)
read.csv(file, header = TRUE, sep = ",", quote = "\"",
dec = ".", fill = TRUE, comment.char = "", ...)
- Note:
- The R function
attach
attaches variables of a dataframe in the global environment. Although the function provides convenience, brevity and simplicity, it is generally not a good programming practice. DO NOT USEattach
in your R script. - Data are read using connection interfaces. Connections are commonly made to files or to other formats like
url
,gzfile
,bzfile
etc.
- The
foreign
library supports reading data in several proprietary formats.
- The R function