darthgervais
2008-May-31 12:57 UTC
[R] Is variable in one vector part of variable in another
I have 2 vectors x & z x has 10 variables in it z has 75 variable in it I need to check all 75 variables in z and see if they are present in the vector x. So far this is what I have: for (i in 0:75){ if (z[i]==x) (w=w+1) } Thanks in advance w -- View this message in context: http://www.nabble.com/Is-variable-in-one-vector-part-of-variable-in-another-tp17574954p17574954.html Sent from the R help mailing list archive at Nabble.com.
Dr. S. B. Nguah
2008-May-31 13:48 UTC
[R] Is variable in one vector part of variable in another
darthgervais wrote:> > I have 2 vectors x & z > x has 10 variables in it > z has 75 variable in it > > I need to check all 75 variables in z and see if they are present in the > vector x. > So far this is what I have: > > for (i in 0:75){ > if (z[i]==x) (w=w+1) > } > > Thanks in advance > w >Try: any(!(z%in%x) # if TRUE then extract with z[!(z%in%x)] ----- Blay S KATH Kumasi, Ghana. -- View this message in context: http://www.nabble.com/Is-variable-in-one-vector-part-of-variable-in-another-tp17574954p17575410.html Sent from the R help mailing list archive at Nabble.com.
Philipp Pagel
2008-May-31 21:27 UTC
[R] Is variable in one vector part of variable in another
> I have 2 vectors x & z > x has 10 variables in it > z has 75 variable in it > > I need to check all 75 variables in z and see if they are present in the > vector x.In addition to the solution using %in% which has already been suggested, you may also want to play with something like this: # make up some data x = sample(1:8, 10, rep=T) z = sample(1:25, 75, rep=T) # find intersection between x and z intersect(x,z) cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany and Institut f?r Bioinformatik und Systembiologie / MIPS Helmholtz Zentrum M?nchen - Deutsches Forschungszentrum f?r Gesundheit und Umwelt Ingolst?dter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/staff/pagel
Bert Gunter
2008-Jun-02 15:38 UTC
[R] Is variable in one vector part of variable in another
... Indeed, but note that both %in% and intersect() are essentially wrappers for match() . Cheers, Bert Gunter -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Philipp Pagel Sent: Saturday, May 31, 2008 2:27 PM To: r-help at r-project.org Subject: Re: [R] Is variable in one vector part of variable in another> I have 2 vectors x & z > x has 10 variables in it > z has 75 variable in it > > I need to check all 75 variables in z and see if they are present in the > vector x.In addition to the solution using %in% which has already been suggested, you may also want to play with something like this: # make up some data x = sample(1:8, 10, rep=T) z = sample(1:25, 75, rep=T) # find intersection between x and z intersect(x,z) cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany and Institut f?r Bioinformatik und Systembiologie / MIPS Helmholtz Zentrum M?nchen - Deutsches Forschungszentrum f?r Gesundheit und Umwelt Ingolst?dter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/staff/pagel ______________________________________________ R-help at r-project.org 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.