By any chance is there an R command to compare two vectors? For example, a_tmp<-c("a", "b", "c", "d", "e", "f", "g", 'h') b_tmp<-c("a", "c", "e", "g") I would like to compare b_tmp against a_tmp to determine if the members of b_tmp are part of a_tmp. I tried subset(b_tmp, b_tmp==a_tmp) That doesn't seem to work. Thanks again. [[alternative HTML version deleted]]
Dear Jason, Yes, here is one way:> a_tmp<-c("a", "b", "c", "d", "e", "f", "g", 'h') > b_tmp<-c("a", "c", "e", "g") > b_tmp %in% a_tmp[1] TRUE TRUE TRUE TRUE> b_tmp[b_tmp%in%a_tmp][1] "a" "c" "e" "g"> all(b_tmp %in% a_tmp)[1] TRUE Take a look at ?"%in%" and ?all for more information. HTH, Jorge On Wed, Feb 4, 2009 at 6:49 PM, Jason Rupert <jasonkrupert@yahoo.com> wrote:> By any chance is there an R command to compare two vectors? > > For example, > a_tmp<-c("a", "b", "c", "d", "e", "f", "g", 'h') > b_tmp<-c("a", "c", "e", "g") > > I would like to compare b_tmp against a_tmp to determine if the members of > b_tmp are part of a_tmp. > > I tried > subset(b_tmp, b_tmp==a_tmp) > > That doesn't seem to work. > > Thanks again. > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >[[alternative HTML version deleted]]
Try:> b_tmp %in% a_tmpYou may also want to use the all or any function with the above. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Jason Rupert > Sent: Wednesday, February 04, 2009 4:50 PM > To: R-help at r-project.org > Subject: [R] R command to compare two vectors? > > By any chance is there an R command to compare two vectors? > > For example, > a_tmp<-c("a", "b", "c", "d", "e", "f", "g", 'h') > b_tmp<-c("a",?"c", "e", "g") > > I would like to compare b_tmp against a_tmp to determine if the members > of b_tmp are part of a_tmp. > > I tried > ?subset(b_tmp, b_tmp==a_tmp) > > That doesn't seem to work. > > Thanks again. > > > > [[alternative HTML version deleted]]
Seeliger.Curt at epamail.epa.gov
2009-Feb-05 00:02 UTC
[R] R command to compare two vectors?
Jason Rupert writes:> By any chance is there an R command to compare two vectors? > > For example, > a_tmp<-c("a", "b", "c", "d", "e", "f", "g", 'h') > b_tmp<-c("a", "c", "e", "g") > > I would like to compare b_tmp against a_tmp to determine if the > members of b_tmp are part of a_tmp. > > I tried subset(b_tmp, b_tmp==a_tmp)It depends on what you want to do. identical() will give you simple TRUE/FALSE results, setdiff() will list elements only found in one vector. Using the %in% operator is another good possibility. cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.curt@epa.gov 541/754-4638 [[alternative HTML version deleted]]