Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a vector of lists in R?

Tags:

list

r

vector

I have a list (tmpList), which looks like this:

$op
[1] "empty"

$termset
$termset$field
[1] "entry"

$termset[[2]]
$termset[[2]]$explode
[1] "Y"

This is a list with a list inside. If I add this list to a vector

theOneVector = c(theOneVector, tmpList)

Now the resulting vector is of the length 2, because the first entry ("op") of the list is separated from the tmpList. Is it possible to append the complete tmpList into this vector?
I already tried it with

theOneVector = c(theOneVector, list(tmpList))

which gives a vector with the length of 1, but it is very cumbersome to access the elements of the list with this extra list around the list. (Too much list in one sentence I think.)

Any help would be appreciated,
Martin

like image 395
Martin Avatar asked Apr 12 '10 19:04

Martin


People also ask

Can you create a vector of lists in R?

How to create a list in R programming? List can be created using the list() function. Here, we create a list x , of three components with data types double , logical and integer vector respectively. Its structure can be examined with the str() function.

How do I vector a list in R?

To convert List to Vector in R, use the unlist() function. The unlist() function simplifies to produce a vector by preserving all atomic components.

Can you have a vector of lists?

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.

How do you make multiple vectors in R?

To create combination of multiple vectors, we can use expand. grid function. For example, if we have six vectors say x, y, z, a, b, and c then the combination of vectors can be created by using the command expand. grid(x,y,z,a,b,c).


1 Answers

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.

like image 87
Dirk Eddelbuettel Avatar answered Oct 14 '22 00:10

Dirk Eddelbuettel