Hello R-ologists, Imagine you have a list "list" like so:>list[[1]] [1] "IPI00776145.1" "IPI00776187.1" [[2]] [1] "Something" "IPI00807764.1" "IPI00807887.1" [[3]] [1] "IPI00807764.1" [[4]] [1] "Somethingelse" What I need to achieve is a filtered list "list2" like so:>list2[[1]] [1] "IPI00776145.1" [[2]] [1] "IPI00807764.1" [[3]] [1] "IPI00807764.1" 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. Can anybody nudge me towards an elegant solution without looping - I have LOTS of entries to process ... Thanks for your Teachings, Joh
try this: lis. <- lapply(lis, function(x) if (length(ind <- grep("^IPI", x))) x[ind[1]] else NULL) lis.[!sapply(lis., is.null)] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Johannes Graumann" <johannes_graumann at web.de> To: <r-help at stat.math.ethz.ch> Sent: Thursday, February 22, 2007 3:33 PM Subject: [R] List filtration> Hello R-ologists, > > Imagine you have a list "list" like so: > >>list > [[1]] > [1] "IPI00776145.1" "IPI00776187.1" > > [[2]] > [1] "Something" "IPI00807764.1" "IPI00807887.1" > > [[3]] > [1] "IPI00807764.1" > > [[4]] > [1] "Somethingelse" > > What I need to achieve is a filtered list "list2" like so: > >>list2 > [[1]] > [1] "IPI00776145.1" > > [[2]] > [1] "IPI00807764.1" > > [[3]] > [1] "IPI00807764.1" > > 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. > > Can anybody nudge me towards an elegant solution without looping - I > have > LOTS of entries to process ... > > Thanks for your Teachings, > > Joh > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
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.
Thanks for your help! Joh Dimitris Rizopoulos wrote:> try this: > > lis. <- lapply(lis, function(x) if (length(ind <- grep("^IPI", x))) > x[ind[1]] else NULL) > lis.[!sapply(lis., is.null)] > > > I hope it helps. > > Best, > Dimitris > > ---- > Dimitris Rizopoulos > Ph.D. Student > Biostatistical Centre > School of Public Health > Catholic University of Leuven > > Address: Kapucijnenvoer 35, Leuven, Belgium > Tel: +32/(0)16/336899 > Fax: +32/(0)16/337015 > Web: http://med.kuleuven.be/biostat/ > http://www.student.kuleuven.be/~m0390867/dimitris.htm > > > ----- Original Message ----- > From: "Johannes Graumann" <johannes_graumann at web.de> > To: <r-help at stat.math.ethz.ch> > Sent: Thursday, February 22, 2007 3:33 PM > Subject: [R] List filtration > > >> Hello R-ologists, >> >> Imagine you have a list "list" like so: >> >>>list >> [[1]] >> [1] "IPI00776145.1" "IPI00776187.1" >> >> [[2]] >> [1] "Something" "IPI00807764.1" "IPI00807887.1" >> >> [[3]] >> [1] "IPI00807764.1" >> >> [[4]] >> [1] "Somethingelse" >> >> What I need to achieve is a filtered list "list2" like so: >> >>>list2 >> [[1]] >> [1] "IPI00776145.1" >> >> [[2]] >> [1] "IPI00807764.1" >> >> [[3]] >> [1] "IPI00807764.1" >> >> 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. >> >> Can anybody nudge me towards an elegant solution without looping - I >> have >> LOTS of entries to process ... >> >> Thanks for your Teachings, >> >> Joh >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html and provide commented, > minimal, self-contained, reproducible code.