Title essentially says it all. I'm having trouble figuring out the difference between initializing a vector with vector(mode="list")
and a list with list()
.
There are some minor differences in the signatures, list()
can take value
arguments or tag = value
arguments whereas vector()
cannot.
And then there's the following quote from the list()
documentation:
Almost all lists in R internally are Generic Vectors
So is there any actual difference beside the fact that lists can be initialized with tags and values?
I'd say they're the same:
identical(list(),vector(mode="list", length=0))
## [1] TRUE
(see also this question about the confusing fact that a list is a vector in R: usually when R users refer to "vectors", they actually mean atomic vectors ...)
In my experience the most common use case for vector(mode="list",...)
is when you want to initialize a list with length>0. vector(mode="list",10)
might be a little more expressive than replicate(10,NULL)
. If you want to create a length-0 list I can't see any reason to use vector()
instead of list()
.
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