[R] shrink list by mathed entries

David Winsemius dwinsemius at comcast.net
Sat Nov 14 03:58:07 CET 2009


On Nov 13, 2009, at 11:19 AM, Soeren.Vogel at eawag.ch wrote:

> Hello
>
> a <- c("Mama", "Papa", "Papa; Mama", "", "Sammy; Mama; Papa")
> a <- strsplit(a, "; ")
> mama <- rep(F, length(a))
> mama[sapply(a, function(x) { sum(x=="Mama") }, simplify=T) > 0] <- T
> papa <- rep(F, length(a))
> papa[sapply(a, function(x) { sum(x=="Papa") }, simplify=T) > 0] <- T
> # ... more variables
>
> ... produces the variables "mama" and "papa" correctly. But how do I  
> remove all "Mama" list entries in "a" in the same run, that is,  
> shrink the list by what was already matched?

Maybe you should explain what you were trying to do?  Perhaps:

 > a[!mama]
[[1]]
[1] "Papa"

[[2]]
character(0)

I would sidestep that confusing sequence of logical assignments and  
just do this:

 > a[ -grep("Mama", a) ]
[[1]]
[1] "Papa"

[[2]]
character(0)

>
-- 
David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list