A list is (in most cases) itself a vector. From the help files for ?list
: "Most lists in R internally are Generic Vectors, whereas traditional dotted pair lists (as in LISP) are available but rarely seen by users (except as formals of functions)."
This means you can use vector
to pre-allocate memory for a list:
x <- vector("list", 3)
class(x)
[1] "list"
Now allocate a value to the second element in the list:
x[[2]] <- 1:5
x
[[1]]
NULL
[[2]]
[1] 1 2 3 4 5
[[3]]
NULL
See ?list
and ?vector
for more details.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…