I'm an R beginner. Browsing the R documentation, I stumbled upon this sentence ?is.vector
: "If mode = "any", is.vector may return TRUE for the atomic modes, list and expression."
I'm just curious - why? All of the documentation I've read states that lists and vectors are two different data types. Is there some deeper R datatype concept I'm not getting?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With