Hello, I have the following very simple problem: Say I have two vectors, a<-c(1,7,4,5,9,11) b<-c(7,4,9) I would like to create a vector containing the elements in a which are not in b. Obviously, this is possible by writing a[a!=b[1] & a!=b[2] & a!=b[3]] But I would like a solution which is applicable to the situation where the number of elements in b is unknown. I have looked in the R manuals, the FAQ and the mailing lists, but have been unable to find a solution. Thank you for your replies, Alexander
Alexander -- a[!(a %in% b)] should do the trick. setdiff could also probably be used. Hope this helps, Matt Wiener -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Alexander Sokol Sent: Thursday, November 11, 2004 8:34 AM To: r-help at stat.math.ethz.ch Subject: [R] Logical "and" Hello, I have the following very simple problem: Say I have two vectors, a<-c(1,7,4,5,9,11) b<-c(7,4,9) I would like to create a vector containing the elements in a which are not in b. Obviously, this is possible by writing a[a!=b[1] & a!=b[2] & a!=b[3]] But I would like a solution which is applicable to the situation where the number of elements in b is unknown. I have looked in the R manuals, the FAQ and the mailing lists, but have been unable to find a solution. Thank you for your replies, Alexander ______________________________________________ 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
On Nov 11, 2004, at 8:33 AM, Alexander Sokol wrote:> Say I have two vectors, > > a<-c(1,7,4,5,9,11) > b<-c(7,4,9) > > I would like to create a vector containing the elements in a which are > not in > b.> a[!(a %in% b)] [1] 1 5 11 Sean
a[!is.element(a, b)] Jussi -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Alexander Sokol Sent: 11. marraskuuta 2004 15:34 To: r-help at stat.math.ethz.ch Subject: [R] Logical "and" Hello, I have the following very simple problem: Say I have two vectors, a<-c(1,7,4,5,9,11) b<-c(7,4,9) I would like to create a vector containing the elements in a which are not in b. Obviously, this is possible by writing a[a!=b[1] & a!=b[2] & a!=b[3]] But I would like a solution which is applicable to the situation where the number of elements in b is unknown. I have looked in the R manuals, the FAQ and the mailing lists, but have been unable to find a solution. Thank you for your replies, Alexander ______________________________________________ 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
How about this? a<-c(1,7,4,5,9,11) b<-c(7,4,9) a[!a %in% b] b<-c(7,4,9, 100, 20, 34, 54) a[!a %in% b] see ?match, too HTH, Andy> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Alexander Sokol > Sent: Thursday, November 11, 2004 8:34 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Logical "and" > > > Hello, > > I have the following very simple problem: > > Say I have two vectors, > > a<-c(1,7,4,5,9,11) > b<-c(7,4,9) > > I would like to create a vector containing the elements in a > which are not in > b. > > Obviously, this is possible by writing > > a[a!=b[1] & a!=b[2] & a!=b[3]] > > But I would like a solution which is applicable to the situation > where the > number of elements in b is unknown. > > I have looked in the R manuals, the FAQ and the mailing lists, > but have been > unable to find a solution. > > Thank you for your replies, > Alexander > > ______________________________________________ > 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 >
Alexander Sokol <alexandersokol at ofir.dk> writes:> Hello, > > I have the following very simple problem: > > Say I have two vectors, > > a<-c(1,7,4,5,9,11) > b<-c(7,4,9) > > I would like to create a vector containing the elements in a which are not in > b.As in> setdiff(a,b)[1] 1 5 11 or> a[!(a %in% b)][1] 1 5 11 ? (Note that they differ if a has nonunique values). -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Thu, 11 Nov 2004, Alexander Sokol wrote: ?setdiff> setdiff(a,b)[1] 1 5 11> Hello, > > I have the following very simple problem: > > Say I have two vectors, > > a<-c(1,7,4,5,9,11) > b<-c(7,4,9) > > I would like to create a vector containing the elements in a which are not in > b. > > Obviously, this is possible by writing > > a[a!=b[1] & a!=b[2] & a!=b[3]] > > But I would like a solution which is applicable to the situation where the > number of elements in b is unknown. > > I have looked in the R manuals, the FAQ and the mailing lists, but have been > unable to find a solution. > > Thank you for your replies, > Alexander > > ______________________________________________ > 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 >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
The answer is the function setdiff(), but I suppose you have to know what the operation is called to find it.> setdiff(a, b)[1] 1 5 11 You may find it illuminating to see how it is implemented. On Thu, 11 Nov 2004, Alexander Sokol wrote:> Hello, > > I have the following very simple problem: > > Say I have two vectors, > > a<-c(1,7,4,5,9,11) > b<-c(7,4,9) > > I would like to create a vector containing the elements in a which are not in > b. > > Obviously, this is possible by writing > > a[a!=b[1] & a!=b[2] & a!=b[3]] > > But I would like a solution which is applicable to the situation where the > number of elements in b is unknown. > > I have looked in the R manuals, the FAQ and the mailing lists, but have been > unable to find a solution.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hi On 11 Nov 2004 at 14:33, Alexander Sokol wrote:> Hello, > > I have the following very simple problem: > > Say I have two vectors, > > a<-c(1,7,4,5,9,11) > b<-c(7,4,9) > > I would like to create a vector containing the elements in a which are > not in b.> a%in%b[1] FALSE TRUE TRUE FALSE TRUE FALSE> !a%in%b[1] TRUE FALSE FALSE TRUE FALSE TRUE> a[!a%in%b][1] 1 5 11>Is it OK? Cheers Petr> > Obviously, this is possible by writing > > a[a!=b[1] & a!=b[2] & a!=b[3]] > > But I would like a solution which is applicable to the situation where > the number of elements in b is unknown. > > I have looked in the R manuals, the FAQ and the mailing lists, but > have been unable to find a solution. > > Thank you for your replies, > Alexander > > ______________________________________________ > 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.htmlPetr Pikal petr.pikal at precheza.cz
Try setdiff(a, b) -roger Alexander Sokol wrote:> Hello, > > I have the following very simple problem: > > Say I have two vectors, > > a<-c(1,7,4,5,9,11) > b<-c(7,4,9) > > I would like to create a vector containing the elements in a which are not in > b. > > Obviously, this is possible by writing > > a[a!=b[1] & a!=b[2] & a!=b[3]] > > But I would like a solution which is applicable to the situation where the > number of elements in b is unknown. > > I have looked in the R manuals, the FAQ and the mailing lists, but have been > unable to find a solution. > > Thank you for your replies, > Alexander > > ______________________________________________ > 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 >-- Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/
Alexander Sokol wrote:> Hello, > > I have the following very simple problem: > > Say I have two vectors, > > a<-c(1,7,4,5,9,11) > b<-c(7,4,9) > > I would like to create a vector containing the elements in a which are not in > b. > > Obviously, this is possible by writing > > a[a!=b[1] & a!=b[2] & a!=b[3]]a[!(a %in% b)] or see ?setdiff Uwe Ligges> But I would like a solution which is applicable to the situation where the > number of elements in b is unknown. > > I have looked in the R manuals, the FAQ and the mailing lists, but have been > unable to find a solution. > > Thank you for your replies, > Alexander > > ______________________________________________ > 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
You can use setdiff if you only need the unique values of a that are not in b. If you want all values you can use a[a%in%setdiff(a,b)] There are also intersection, union etc... see ?setdiff On Thu, 11 Nov 2004, Alexander Sokol wrote:> Hello, > > I have the following very simple problem: > > Say I have two vectors, > > a<-c(1,7,4,5,9,11) > b<-c(7,4,9) > > I would like to create a vector containing the elements in a which are not in > b. > > Obviously, this is possible by writing > > a[a!=b[1] & a!=b[2] & a!=b[3]] > > But I would like a solution which is applicable to the situation where the > number of elements in b is unknown. > > I have looked in the R manuals, the FAQ and the mailing lists, but have been > unable to find a solution. > > Thank you for your replies, > Alexander > > ______________________________________________ > 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 >