1

I have list of position index in the IntegerList, and I intended to filter them given threshold, and it works well. However, I want to extract out one of specific filtered set for each IntegerList for further usage. I aware that myList is nested list , and data are very much simulated based on real data set. Is there any way to retrieve wanted IntegerList easily and elegantly? How can I make it this extraction happen?

To run mini example, following library is needed:

library(IRanges)
library(S4Vectors)

mini example :

myList <- list(f1=IntegerList(1,2,3,4,1,1,1,integer(0),1,2,4),
               f2=IntegerList(1,5,integer(0),integer(0),2,3,4,6,1,5,6),
               f3=IntegerList(1,4,6,7,2,3,3,7,2,5,7))

len <- Reduce('+', lapply(myList, lengths))
keepMe <- len >= length(myList)

I intended to filter them as follow:

res.filt <- lapply(myList, function(elm) {
  ans <- list(keep=elm[keepMe], droped=elm[!keepMe])
  ans
})

my rough output :

Keep.list <- list(f1.kp=res.filt$f1$keep, f2.kp=res.filt$f2$keep, f3.kp=res.filt$f3$keep)
Drop.list <- list(f1.dp=res.filt$f1$droped, f2.dp=res.filt$f2$droped, f3.dp=res.filt$f3$droped)

Based on my rough output, How can I get more elegant output ? any efficient way to achieve my output ? Can anyone point me how to do? Or any suggestion how to get my expected output ? Thanks in advance

Hack-R
  • 22,422
  • 14
  • 75
  • 131
Andy.Jian
  • 417
  • 3
  • 15

1 Answers1

2

Your thought process/flow for filtering the vector lists is logical and pretty optimal, but you can tighten it up a bit if you use purrr:

library(purrr)

map(myList, lengths) %>% 
  reduce(`+`) %>% 
  map_lgl(`>=`, length(myList)) -> keep_me

keep_list <- map(myList, ~.[keep_me])
drop_list <- map(myList, ~.[!keep_me])
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • This is wonderful solution. I give me credits to developer of purrr packages. Thank you so much :) – Andy.Jian Oct 01 '16 at 15:42
  • 1
    You're welcome! Though this is actually just doing what you were doing. The code is just a tad more compact. You had a fine, working solution. – hrbrmstr Oct 01 '16 at 15:49