Skip to content

Filtering R lists

6 messages · Nikol Simecek, PIKAL Petr, Jorge Ivan Velez +1 more

#
Hello
I am am new to R and any help with the following would be appreciated:
I have a list (example attached) and I would like to create a new list 
which is a filtered version of this list. I.e I would like a list that 
only contains elements with this value:

Chr 10 : 21853562 - 21855482

Any pointers/tips would be great.
Thanks!
Nikol
#
Hi


r-help-bounces at r-project.org napsal dne 04.03.2009 13:18:30:
Lists can have quite complicated structure so here is only a small 
suggestion

lapply(your.list, function(x) x==desired.value)

This shall give you list of logical values if your list has only one 
level. 

Something like

pts <- list(x=mtcars[,1], y=mtcars[,2], z=mtcars[,10])
pts[which(unlist(lapply(pts, function(x) sum(x==4)>0)))]

Regards
Petr
inSymbol, type
inSymbol, type
inSymbol, type
inSymbol, type
inSymbol, type
http://www.R-project.org/posting-guide.html
#
Nikol Simecek wrote:
Filter provides a generic approach to filtering:

    Filter(x=<your list>,
        f=function(element)
            <does element contain 'Chr 10 : 21853562 - 21855482'?>)

where you have to implement the filtering function.

if each element in your list is a string (a character vector of length
1), you can also use grep:

    grep('Chr 10 : 21853562 - 21855482', <your list>, value=TRUE)

if each element in your list is a character vector, a list, a data
frame, or something else (not just a single string), you may use a
combination of grep and lapply, but i think the Filter approach is in
this case cleaner.

see ?Filter and ?grep.

vQ
#
Petr PIKAL wrote:
pts[sapply(pts, function(x) sum(x==4)>0)]

will gladly do the same.

vQ