Search
Question: How to coerce CompressedIntegerList as vector?
0
21 months ago by
jian_liangli0 wrote:

Hi,

I have a list where each list element is IntegerList, I want to coerce IntegerList object to vector. I tried of using as.vector function, but it is still not simplified as I expected. How can make this happen? Any idea?

I tried as follow, but output is not what I expected:

lapply(myList, as.vector)

# mini example:

myList <- list(
a=IntegerList(1,2,3,4),
b=IntegerList(3,4,integer(0),integer(0)),
c=IntegerList(1,4,6,7)
)


# desired output:

desired_output <- list(
a=c(1,2,3,4),
b=c(3,4,0,0),
c=c(1,4,6,7)
)

How can I achieve me desired output? Can anyone propose possible idea to make this coercion happen easily? Thanks

modified 21 months ago • written 21 months ago by jian_liangli0
2
21 months ago by
Michael Lawrence10.0k
United States
Michael Lawrence10.0k wrote:

as.vector() behaves the same way on Lists as it does on ordinary lists.

Are you sure that each IntegerList has zero or one elements? If so, you can use drop(), as in:

lapply(myList, drop)