Section 9 Read Data
9.1 Function
Function | Explanation |
---|---|
setwd |
Set the working directory. Note the file path is / or \\ . |
getwd |
Get the current working directory. Note the file path is / 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. |
read.csv |
Read a csv file |
readLines |
Read lines of a text file |
source |
Read the R script file |
9.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 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 in 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
RStudio
GUI supports reading data in several proprietary formats like SPSS, SAS and Stata.
- The function