Database Reference
In-Depth Information
a backslash (\) as a separator. To simplify the import of multiple files with long
path names, the setwd() function can be used to set the working directory for the
subsequent import and export operations, as shown in the following R code.
setwd("c:/data/")
sales <- read.csv("yearly_sales.csv")
Other import functions include read.table() and read.delim() , which are
intended to import other common file types such as TXT. These functions can also
be used to import the yearly_sales .csv file, as the following code illustrates.
sales_table <- read.table("yearly_sales.csv", header=TRUE,
sep=",")
sales_delim <- read.delim("yearly_sales.csv", sep=",")
The main difference between these import functions is the default values. For
example, the read .delim() function expects the column separator to be a
tab (" \t "). In the event that the numerical data in a data file uses a comma
for the decimal, R also provides two additional functions— read.csv2() and
read.delim2() —to import such data. Table 3.1 includes the expected defaults
for headers, column separators, and decimal point notations.
Table 3.1 Import Function Defaults
Function Headers Separator Decimal Point
read.table() FALSE
“ ”
“.”
TRUE
“,”
“.”
read.csv()
TRUE
“;”
“,”
read.csv2()
read.delim() TRUE
“\t”
“.”
read.delim2() TRUE
“\t”
“,”
The analogous R functions such as write.table() , write.csv() , and
write.csv2() enable exporting of R datasets to an external file. For example, the
following R code adds an additional column to the sales dataset and exports the
modified dataset to an external file.
# add a column for the average sales per order
sales$per_order <- sales$sales_total/sales$num_of_orders
Search WWH ::




Custom Search