I have an ordered "set" of numbers, represented by a vector, say > X <- c(10:13, 17,18) > X [1] 10 11 12 13 17 18 then I have a "sub-set" of X, say > Y <- c(11,12,17,18) Is there a simple way in R to have a logical vector (parallel to X) indicating what elements of X are in Y, i.e., > Inclusion [1] FALSE TRUE TRUE FALSE TRUE TRUE I'm trying to avoid looping over both vectors to produce the tests. Do you have any comments on this? Thanks, --Sergio.
At least two ways> (!is.na(match(X, Y)))[1] FALSE TRUE TRUE FALSE TRUE TRUE> X %in% Y[1] FALSE TRUE TRUE FALSE TRUE TRUE>On Thu, Apr 5, 2012 at 3:32 PM, Julio Sergio <juliosergio@gmail.com> wrote:> I have an ordered "set" of numbers, represented by a vector, say > > > X <- c(10:13, 17,18) > > X > [1] 10 11 12 13 17 18 > > then I have a "sub-set" of X, say > > > Y <- c(11,12,17,18) > > Is there a simple way in R to have a logical vector (parallel to X) > indicating > what elements of X are in Y, i.e., > > > Inclusion > [1] FALSE TRUE TRUE FALSE TRUE TRUE > > I'm trying to avoid looping over both vectors to produce the tests. Do you > have > any comments on this? > > Thanks, > > --Sergio. > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Richard M. Heiberger <rmh <at> temple.edu> writes:> > At least two ways > > > (!is.na(match(X, Y))) > [1] FALSE TRUE TRUE FALSE TRUE TRUE > > X %in% Y > [1] FALSE TRUE TRUE FALSE TRUE TRUEThanks Richard! --Sergio.
On 05-04-2012, at 21:32, Julio Sergio wrote:> I have an ordered "set" of numbers, represented by a vector, say > >> X <- c(10:13, 17,18) >> X > [1] 10 11 12 13 17 18 > > then I have a "sub-set" of X, say > >> Y <- c(11,12,17,18) > > Is there a simple way in R to have a logical vector (parallel to X) indicating > what elements of X are in Y, i.e., > >> Inclusion > [1] FALSE TRUE TRUE FALSE TRUE TRUE > > I'm trying to avoid looping over both vectors to produce the tests. Do you have > any comments on this?Try X %in% Y You could also have a look at match Berend