Search
Question: How to coerce CompressedIntegerList as vector?
0
gravatar for jian_liangli
16 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

ADD COMMENTlink modified 16 months ago • written 16 months ago by jian_liangli0
2
gravatar for Michael Lawrence
16 months ago by
United States
Michael Lawrence9.8k 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)

 

ADD COMMENTlink written 16 months ago by Michael Lawrence9.8k

Thanks, your answer helped.

ADD REPLYlink modified 16 months ago • written 16 months ago by jian_liangli0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.2.0
Traffic: 302 users visited in the last hour