Recursive Lists
Lists can be recursive, meaning that you can have lists within lists. Here’s an example:
> b <- list(u = 5, v = 12) > c <- list(w = 13) > a <- list(b,c) > a [[1]] [[1]]$u [1] 5 [[1]]$v [1] 12 [[2]] [[2]]$w [1] 13 > length(a) [1] 2
This code makes a
into a two-component list, with each component itself also being a list.
The concatenate function c()
has an optional argument recursive
, which controls whether flattening occurs 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 1 2 5 9
In the first case, we accepted the default value of recursive
, which is FALSE
, and obtained a recursive list, with the c
component ...
Get The Art of R Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.