Hello, Imagine 3 lists like so:> a <- list("A","B","C","D") > b <- list("A","B","E","F") > c <- list("A","C","E","G")What I need (vennDiagram) is a matrix characterizing with 1 or 0 whether any given member is present or not like so: x1 x2 x3 [1,] 1 1 1 [2,] 1 1 0 [3,] 1 0 1 [4,] 1 0 0 [5,] 0 1 1 [6,] 0 1 0 [7,] 0 0 1 (where the rows represent "A"-"G" and the columns a-c, respectively).> table(c(a,b,c))will give me a quick answer for the "1 1 1" case, but how to deal with the other cases efficiently without looping over each string and looking for membership %in% each list? Thanks for enlightening the learning, Joh
> a <- c("A","B","C","D") > b <- c("A","B","E","F") > c <- c("A","C","E","G") > Df <- cbind(a, b, c) > apply(Df, 2, function(x)(LETTERS[1:7] %in% x))a b c [1,] TRUE TRUE TRUE [2,] TRUE TRUE FALSE [3,] TRUE FALSE TRUE [4,] TRUE FALSE FALSE [5,] FALSE TRUE TRUE [6,] FALSE TRUE FALSE [7,] FALSE FALSE TRUE> > apply(Df, 2, function(x)(as.numeric(LETTERS[1:7] %in% x)))a b c [1,] 1 1 1 [2,] 1 1 0 [3,] 1 0 1 [4,] 1 0 0 [5,] 0 1 1 [6,] 0 1 0 [7,] 0 0 1 Cheers, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney> -----Oorspronkelijk bericht----- > Van: r-help-bounces op stat.math.ethz.ch [mailto:r-help- > bounces op stat.math.ethz.ch] Namens Johannes Graumann > Verzonden: maandag 26 februari 2007 16:25 > Aan: r-help op stat.math.ethz.ch > Onderwerp: [R] Test of Presence Matrix HOWTO? > > Hello, > > Imagine 3 lists like so: > > > a <- list("A","B","C","D") > > b <- list("A","B","E","F") > > c <- list("A","C","E","G") > > What I need (vennDiagram) is a matrix characterizing with 1 or 0whether> any > given member is present or not like so: > x1 x2 x3 > [1,] 1 1 1 > [2,] 1 1 0 > [3,] 1 0 1 > [4,] 1 0 0 > [5,] 0 1 1 > [6,] 0 1 0 > [7,] 0 0 1 > > (where the rows represent "A"-"G" and the columns a-c, respectively). > > > table(c(a,b,c)) > will give me a quick answer for the "1 1 1" case, but how to deal withthe> other cases efficiently without looping over each string and lookingfor> membership %in% each list? > > Thanks for enlightening the learning, > > Joh > > ______________________________________________ > R-help op 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.
you can use something like the following: a <- list("A","B","C","D") b <- list("A","B","E","F") c <- list("A","C","E","G") ##################### abc <- list(a, b, c) unq.abc <- unique(unlist(abc)) out.lis <- lapply(abc, "%in%", x = unq.abc) out.lis lapply(out.lis, as.numeric) 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: Monday, February 26, 2007 4:25 PM Subject: [R] Test of Presence Matrix HOWTO?> Hello, > > Imagine 3 lists like so: > >> a <- list("A","B","C","D") >> b <- list("A","B","E","F") >> c <- list("A","C","E","G") > > What I need (vennDiagram) is a matrix characterizing with 1 or 0 > whether any > given member is present or not like so: > x1 x2 x3 > [1,] 1 1 1 > [2,] 1 1 0 > [3,] 1 0 1 > [4,] 1 0 0 > [5,] 0 1 1 > [6,] 0 1 0 > [7,] 0 0 1 > > (where the rows represent "A"-"G" and the columns a-c, > respectively). > >> table(c(a,b,c)) > will give me a quick answer for the "1 1 1" case, but how to deal > with the > other cases efficiently without looping over each string and looking > for > membership %in% each list? > > Thanks for enlightening the learning, > > 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