The Art of R Programming

(WallPaper) #1
The Boolean valuesTRUEandFALSEcan be abbreviated asTandF
(both must be capitalized). These values change to 1 and 0 in arithmetic
expressions:

>1<2
[1] TRUE
>(1<2)*(3<4)
[1] 1
>(1<2)*(3<4)*(5<1)
[1] 0
> (1 < 2) == TRUE
[1] TRUE
>(1<2)==1
[1] TRUE

In the second computation, for instance, the comparison1<2returns
TRUE, and3<4yieldsTRUEas well. Both values are treated as 1 values, so the
product is 1.
On the surface, R functions look similar to those of C, Java, and so on.
However, they have much more of a functional programming flavor, which
has direct implications for the R programmer.

7.3 Default Values for Arguments................................................


In Section 5.1.2, we read in a data set from a file namedexams:

> testscores <- read.table("exams",header=TRUE)

The argumentheader=TRUEtells R that we have a header line, so R should
not count that first line in the file as data.
This is an example of the use ofnamed arguments. Here are the first few
lines of the function:

> read.table
function (file, header = FALSE, sep = "", quote = "\"'", dec = ".",
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(), encoding = "unknown")
{
if (is.character(file)) {
file <- file(file, "r")
on.exit(close(file))
...
...

146 Chapter 7

Free download pdf