Hi :
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?
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 did following filtering:
res.filt <- lapply(myList, function(elm) { ans <- list(keep=elm[keepMe], droped=elm[!keepMe]) ans })
my rough attempt output :
wantedKeep.list <- list(f1.kp=res.filt$f1$keep, f2.kp=res.filt$f2$keep, f3.kp=res.filt$f3$keep) wantedDrop.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
A variant is use DataFrame
df = DataFrame(myList) keep = rowSums(sapply(df, lengths)) >= ncol(df) as.list(df[keep,, drop=FALSE])
but the short version of jian-liangli's original filtering + rough attempt seems clean enough
Thank you Martin, both answer is very well done.
I edited my answer with a simplification possible with S4Vectors 0.11.18.
Dear Michael:
Thanks again your favor to address my issue. However, I must avoid of converting to NA because I have very important minor details that must be taken into consideration for my specific problem, surely
length(NA)
is way different fromlength(integer(0))
to do vector sum. I have to uselength(integer(0)) in
order to efficiently use geometric property of vector list. the form ofmyList
are very much simulated on my specific data set. Is that possible to make your solution more compatible and easier ? Thank you so much