Andy.Jian Andy.Jian - 8 months ago 45
R Question

Any way to extract out named IntegerList more elegantly from nested list?

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:


mini example :

myList <- list(f1=IntegerList(1,2,3,4,1,1,1,integer(0),1,2,4),

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])

my rough output :

Keep.list <- list($f1$keep,$f2$keep,$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


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:


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

keep_list <- map(myList, ~.[keep_me])
drop_list <- map(myList, ~.[!keep_me])