[R] List filtration

Rajarshi Guha rguha at indiana.edu
Thu Feb 22 16:10:46 CET 2007


On Thu, 2007-02-22 at 15:33 +0100, Johannes Graumann wrote:
> Hello R-ologists,
> 
[snip]
> 
> So: 
> - if sublist-entry 1 start with "^IPI" make it the list-entry.
> - otherwise chose the first "^IPI" sublist-entry present.
> - delete the list-entry if not "^IPI" sublist-entry present.

One way to do it would be:

l <- list(c("IPI00776145.1", "IPI00776187.1"),
          c("Something", "IPI00807764.1", "IPI00807887.1"),
          c("IPI00807764.1"),
          c("Somethingelse"))

f <- function(x) {
  r <- grep("^IPI", x, value=TRUE)
  if (length(r) > 0) return(r[1])
  else return(NA)
}

l2 <- unlist(lapply(l, f))
l2 <- l2[!is.na(l2)]

But I'm sure that more elegant solutions will be posted

-------------------------------------------------------------------
Rajarshi Guha <rguha at indiana.edu>
GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
-------------------------------------------------------------------
Writing software is more fun than working.



More information about the R-help mailing list