I have a vector like this
c("1", "a","b")
and I'd like to create this list
list("a"=1,"b"=1)
is there a way to do it in an "apply" style? Thanks.
-k
A list holds different data such as Numeric, Character, logical, etc. Vector stores elements of the same type or converts implicitly. Lists are recursive, whereas vector is not. The vector is one-dimensional, whereas the list is a multidimensional object.
In this article, we will study how to create a list consisting of vectors as elements and how to access, append and delete these vectors to lists. list() function in R creates a list of the specified arguments. The vectors specified as arguments in this function may have different lengths.
Vector is a dynamic array and has the default size. Memory required to store the elements in the List is comparatively large as it holds the element as well as the pointers for the next and previous nodes. Memory required to store the elements in the Vector is lesser than List as it uses memory for the element only.
Using as.list
and setNames
:
x = c("1", "a","b")
as.list(setNames(rep(as.numeric(x[1]), length(x) - 1), x[-1]))
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