How to detect and replace zero index in IntegerList ?
1
0
Entering edit mode
@jurat-shahidin-9488
Last seen 18 months ago
Chicago, IL, USA

Dear all:

I have IntegerList which has different numeric index. in particular, I am trying to figure out how to detect and replace  0 index in IntergerList with integer(0). How can I make this sort of manipulation efficiently? Does anyone knows any trick of doing this? 

This is different in R, and I am expecting latter:

> length(0)
[1] 1
> length(integer(0))
[1] 0

# data

foo <- list(
  idx1 <- IntegerList(1,2,3,0,4,0,0,6),
  idx2 <- IntegerList(1,1,1,2,0,3,4,0),
  idx3 <- IntegerList(1,2,4,4,5,0,6,7)
)

My idea is find out way to detect zero index in IntegerList, if I detect such index then it must be replaced with integer(0). I am trying to figure out which way I can detect zero index in very long IntegerList. This is my very first attempt to solve, but I bet there must have better way to solve it.

tmp <- lapply(foo, function(ele_) {
  res <- sapply(ele_, function(tt) {
    ans <- ifelse(is.na(tt), integer(0), tt)
  })
})

# my desired output

desired_list <- list(
  idx1_ <- IntegerList(1,2,3, integer(0), 4, integer(0), integer(0), 6),
  idx2_ <- IntegerList(1,1,1,2, integer(0), 3, 4, integer(0)),
  idx3_ <- IntegerList(1,2,4,4,5, integer(0), 6, 7)
)

How can I accomplish this easily/elegantly? Is there anyone give me possible idea for doing this? Thanks a lot

iranges integerlist s4 • 654 views
ADD COMMENT
2
Entering edit mode
@michael-lawrence-3846
Last seen 7 weeks ago
United States

For a given IntegerList x, do:

x[all(x == 0L)] <- IntegerList(integer(0))

 

ADD COMMENT
0
Entering edit mode

This is perfect. Thank you.

ADD REPLY

Login before adding your answer.

Traffic: 352 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6