Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a vector into a list, each element in the vector as an element in the list

Tags:

r

r-faq

The vector is like this:

c(1,2,3) #[1] 1 2 3 

I need something like this:

list(1,2,3) #[[1]] #[1] 1 # #[[2]] #[1] 2 # #[[3]] #[1] 3 

I tried this:

list(c(1,2,3)) #[[1]] #[1] 1 2 3 
like image 764
qed Avatar asked May 03 '13 16:05

qed


People also ask

How do you convert an atomic vector to a list in R?

In order to convert a vector to a list, R provides us with unlist() function that converts the list into a vector in R. The unlist() function dissolves the list structure fed to it as arguments i.e. it converts the multi-dimensional schema into a a single dimensional schema. use.

How do I turn something into a list in R?

list() Function. as. list() function in R Programming Language is used to convert an object to a list.

Can you create a vector list in R?

You can stick a vector (a restricted structure where all components have to be of the same type) into a list (unrestricted). But you cannot do the reverse. Use lists of lists of lists ... and then use lapply et al to extract.


1 Answers

Simple, just do this:

as.list(c(1,2,3)) 
like image 132
qed Avatar answered Oct 15 '22 18:10

qed