Hi, I searched the archives and did not find a good solution to that. assume I have 10 sets and I want to have the common character elements of them. how could i do that? -- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III
assume t2 is a list of size 11 and each element is a vector of characters. the following codes can get what I wanted but I assume there might be a one-line code for that: t3 <- t2[[1]] for ( i in 2:11){ t3 <- intersect(t2[[i]], t3) } or there is no such "apply"? On 4/24/07, Weiwei Shi <helprhelp at gmail.com> wrote:> Hi, > I searched the archives and did not find a good solution to that. > > assume I have 10 sets and I want to have the common character elements of them. > > how could i do that? > > -- > Weiwei Shi, Ph.D > Research Scientist > GeneGO, Inc. > > "Did you always know?" > "No, I did not. But I believed..." > ---Matrix III >-- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III
have u seen ?merge ? ----- Original Message ---- From: Weiwei Shi <helprhelp@gmail.com> To: R Help <R-help@stat.math.ethz.ch> Sent: Tuesday, April 24, 2007 10:55:51 PM Subject: [R] intersect more than two sets Hi, I searched the archives and did not find a good solution to that. assume I have 10 sets and I want to have the common character elements of them. how could i do that? -- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III ______________________________________________ R-help@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. [[alternative HTML version deleted]]
On Tue, 24 Apr 2007, Weiwei Shi wrote:> Hi, > I searched the archives and did not find a good solution to that. > > assume I have 10 sets and I want to have the common character elements of them. > > how could i do that?list.of.sets <- lapply(1:10,function(x) sample(letters,20)) # for example names( which( table( unlist( lapply( list.of.sets, unique ) ) ) ==10 ) ) or int.rec <- function(x) { if (length(x) == 1 ) { x } else { x <- c(list(intersect(x[[1]],x[[2]])),x[-(1:2)]);Recall(x)} } int.rec(list.of.sets)> > -- > Weiwei Shi, Ph.D > Research Scientist > GeneGO, Inc. > > "Did you always know?" > "No, I did not. But I believed..." > ---Matrix III > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0901
Dear Weiwei Shi, How about using recursion?> intersection <- function(x, y, ...){+ if (missing(...)) intersect(x, y) + else intersect(x, intersection(y, ...)) + }> a <- letters[1:4] > b <- letters[2:5] > c <- letters[3:6] > d <- letters[4:7] > e <- letters[5:8]> intersection(a, b)[1] "b" "c" "d"> intersection(a, b, c)[1] "c" "d"> intersection(a, b, c, d)[1] "d"> intersection(a, b, c, d, e)character(0)> do.call(intersection, list(a, b, c))[1] "c" "d" I hope this helps, John -------------------------------- John Fox, Professor Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Weiwei Shi > Sent: Tuesday, April 24, 2007 2:59 PM > To: R Help > Subject: Re: [R] intersect more than two sets > > assume t2 is a list of size 11 and each element is a vector > of characters. > > the following codes can get what I wanted but I assume there > might be a one-line code for that: > > t3 <- t2[[1]] > for ( i in 2:11){ > t3 <- intersect(t2[[i]], t3) > } > > or there is no such "apply"? > > On 4/24/07, Weiwei Shi <helprhelp at gmail.com> wrote: > > Hi, > > I searched the archives and did not find a good solution to that. > > > > assume I have 10 sets and I want to have the common > character elements of them. > > > > how could i do that? > > > > -- > > Weiwei Shi, Ph.D > > Research Scientist > > GeneGO, Inc. > > > > "Did you always know?" > > "No, I did not. But I believed..." > > ---Matrix III > > > > > -- > Weiwei Shi, Ph.D > Research Scientist > GeneGO, Inc. > > "Did you always know?" > "No, I did not. But I believed..." > ---Matrix III > > ______________________________________________ > 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. >
you could try something like the following: t2 <- lapply(1:11, function(i) c("a", sample(letters[1:5], sample(10, 1), TRUE), "b")) ############ unq.vals <- unique(unlist(t2)) ind <- rowSums(sapply(t2, "%in%", x = unq.vals)) == length(t2) unq.vals[ind] 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 Quoting Weiwei Shi <helprhelp at gmail.com>:> assume t2 is a list of size 11 and each element is a vector of characters. > > the following codes can get what I wanted but I assume there might be > a one-line code for that: > > t3 <- t2[[1]] > for ( i in 2:11){ > t3 <- intersect(t2[[i]], t3) > } > > or there is no such "apply"? > > On 4/24/07, Weiwei Shi <helprhelp at gmail.com> wrote: >> Hi, >> I searched the archives and did not find a good solution to that. >> >> assume I have 10 sets and I want to have the common character >> elements of them. >> >> how could i do that? >> >> -- >> Weiwei Shi, Ph.D >> Research Scientist >> GeneGO, Inc. >> >> "Did you always know?" >> "No, I did not. But I believed..." >> ---Matrix III >> > > > -- > Weiwei Shi, Ph.D > Research Scientist > GeneGO, Inc. > > "Did you always know?" > "No, I did not. But I believed..." > ---Matrix III > > ______________________________________________ > 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