iterator), evaluate an expression for each element,
and return the results. Within the foreach function,
you assign elements to a temporary value, just like
in a for loop. Here is the prototype for the foreach
function: foreach(., .combine, .init, .final=
Description Example Vectors integer Naturally
produced from sequences. Can be coerced with the
integer() function. 5:5 integer(5) double Used to
represent floating-point numbers (numbers with
decimals and large numbers). On most modern
platforms, this wil
parentheses, and curly braces. Separating
Expressions You can write a series of expressions on
separate lines: > x <- 1 > y <- 2 > z <- 3 Alternatively,
you can place them on the same line, separated by
semicolons: > x <- 1; y <- 2; z <- 3 Parentheses The
returned to the console to the print function. Some
functionality is implemented internally within the R
system. These calls are made using the .Internal
function. Many functions use .Internal to call
internal R system code. For example, the graphics
func
Minneapolis 89 10 Austin 87 11 Philadelphia 85 12
San Francisco 84 13 Atlanta 82 14 Los Angeles 80 15
Richardson 80 Data frames are implemented as lists
with class data.frame: >
typeof(top.bacon.searching.cities) [1] "list" >
class(top.bacon.searching.cit
Operators Table 6-2 shows the operators in R used
for accessing objects in a data structure. Table 6-2.
Data structure access notation Syntax Objects
Description x[i] Vectors, lists Returns objects from
object x, described by i. i may be an integer vector
the columns the names c1, c2, and c3. > m <matrix(data=1:12,nrow=4,ncol=3, +
dimnames=list(c("r1","r2","r3","r4"), +
c("c1","c2","c3") > m c1 c2 c3 r1 1 5 9 r2 2 6 10 r3
3 7 11 r4 4 8 12 It is also possible to transform
another data structure into a matri
1 and then press the Enter key. The R interpreter
will first translate this expression into the following
function call: `<-`(x, 1) Next, the interpreter
evaluates this function. It assigns the constant value
1 to the symbol x in the current environment a
example, suppose that you had defined a variable
54 | Chapter 5: An Overview of the R Language
animals pointing to a character vector with four
elements: cow, chicken, pig, and tuba. Here
is a statement that assigns this variable: > animals <c("cow", "chi
array: > # use the eye colors vector we used above >
eye.colors 92 | Chapter 7: R Objects [1] brown blue
blue green brown brown brown Levels: blue brown
green > class(eye.colors) [1] "factor" > # now create
a vector by removing the class: >
eye.colors.int
data.frame(a = c(1, 2, 3, 4, 5), b = c(1, 2, 3, 4) :
arguments imply differing number of rows: 5, 4
Usually, each column is named, and sometimes
rows are named as well. The columns in a data
frame are often referred to as variables. Here is a
simple examp
c("brown","blue","blue","green","brown","brown","
brown") This is a perfectly valid way to represent
the information, but it can become inefficient if you
are working with large names or a large number of
observations. R provides a better way to represent
an alternative to indexing by an integer vector, you
can also index through a logical vector. As a simple
example, lets construct a vector of alternating true
and false elements to apply to v: >
rep(c(TRUE,FALSE),10) [1] TRUE FALSE TRUE FALSE
TRUE FALSE T
statement is used to stop iterating through a loop.
The next statement skips to the next loop iteration
without evaluating the remaining expressions in the
loop body. Finally, R provides for loops, which
iterate through each item in a vector (or a list):
Inf and -Inf If a computation results in a number
that is too big, R will return Inf for a positive number
and -Inf for a negative number (meaning positive
and negative infinity, respectively): Special Values |
57 R Language > 2 ^ 1024 [1] Inf > - 2 ^ 102
the parsed expression (in particular, the else
keyword). Also, notice that the first item in the list is
a symbol. In this case, the symbol refers to the if
function. So, although the syntax for the if-then
statement is different from a function call, the
not null, otherwise Series1, Series2, . 96 |
Chapter 7: R Objects The print method for time
series objects can print pretty results when used
with units of months or quarters (this is enabled by
default and is controlled with the calendar
argument to prin
example, a function call has the form f(arguments),
where f is a function and arguments are the
arguments for the function. Operators | 69 R Syntax
> # negation is a unary operator > -7 [1] -7 > # ? (for
help) is also a unary operator > ?`?` Order of
Oper
option: > # creating a vector from four numbers and
a list of > # three more > v <- c(.295, .300, .250, .
287, list(.102, .200, .303), recursive=TRUE) > v [1]
0.295 0.300 0.250 0.287 0.102 0.200 0.303 But
beware of using a list as an argument, as you will
types when appropriate. R will convert from more
specific types to more general types. For example,
suppose that you define a vector x as follows: > x <c(1, 2, 3, 4, 5) > x [1] 1 2 3 4 5 > typeof(x) [1]
"double" > class(x) [1] "numeric" 58 | Chapter 5: An
functions for addition and subtraction. For example:
> date.I.started.writing <as.Date("2/13/2009","%m/0/%Y") >
date.I.started.writing [1] "2009-02-13" > today <Sys.Date() > today [1] "2009-08-03" > today date.I.started.writing Time difference of 171 days
sion function, and later evaluated with the eval
function. expression(1 + 2) Functions closure An R
function not implemented inside the R system. Most
functions fall into this category. Includes userdefined functions, most functions included with R,
and m
with all the parentheses. Incidentally, the S
language was inspired by LISP and uses many of the
same data structures and evaluation techniques
that are used by LISP interpreters. 65 By default,
numbers in R expressions are interpreted as doubleprecision
As in formulas -> -> Rightward assignment =
Assignment (right to left) <- <- Assignment (right to
left) ? Help (unary and binary) a From the
help(syntax) file For a current list of built-in
operators and their precedence, see the help file for
syntax. Ass
elements by position in the vector using the bracket
notation x[s], where x is the vector from which you
want to return elements and s is a second vector
representing the set of element indices that you
would like to query. You can use an integer vector t