The Art of R Programming

(WallPaper) #1
[1] 5

[[1]]$v
[1] 12

[[2]]
[[2]]$w
[1] 13

> length(a)
[1] 2

This code makesainto a two-component list, with each component itself
also being a list.
The concatenate functionc()has an optional argumentrecursive, which
controls whetherflatteningoccurs when recursive lists are combined.

> c(list(a=1,b=2,c=list(d=5,e=9)))
$a
[1] 1

$b
[1] 2

$c
$c$d
[1] 5

$c$e
[1] 9
> c(list(a=1,b=2,c=list(d=5,e=9)),recursive=T)
a b c.d c.e
1259

In the first case, we accepted the default value ofrecursive, which is
FALSE, and obtained a recursive list, with theccomponent of the main list
itself being another list. In the second call, withrecursiveset toTRUE,wegot
a single list as a result; only the names look recursive. (It’s odd that setting
recursivetoTRUEgives anonrecursivelist.)
Recall that our first example of lists consisted of an employee database.
I mentioned that since each employee was represented as a list, the entire
database would be a list of lists. That is a concrete example of recursive lists.

100 Chapter 4

Free download pdf