Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a list and a vector whose mode is list?

Tags:

r

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?

like image 885
Padaca Avatar asked Aug 19 '19 16:08

Padaca


1 Answers

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().

like image 139
Ben Bolker Avatar answered Nov 04 '22 02:11

Ben Bolker