Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a vector to a list

Tags:

r

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

like image 206
knguyen Avatar asked Dec 08 '10 18:12

knguyen


People also ask

Is a vector the same as a list?

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.

Is it possible to create a list of vectors?

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.

Is vector an array or list?

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.


1 Answers

Using as.list and setNames:

x = c("1", "a","b")
as.list(setNames(rep(as.numeric(x[1]), length(x) - 1), x[-1]))
like image 54
Charles Avatar answered Oct 03 '22 17:10

Charles