[R] Filtering R lists

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Mar 4 15:28:00 CET 2009


Nikol Simecek wrote:
> 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

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



More information about the R-help mailing list