Hello, I'm looking for a solution for the following problem: I have two vectors V1 <- c("apple","honey","milk","bread","butter") V2 <- c("bread","milk") now, I would like to know for each element in V1 if it's equal to one of the elements in V2 I could do: which(V1 == V2[1] | V1 == V2[2]) but what if I don't know the length of V2 and it's content??? Thank you in advance! Antje
Antje wrote:> Hello, > > I'm looking for a solution for the following problem: > I have two vectors > > V1 <- c("apple","honey","milk","bread","butter") > V2 <- c("bread","milk") > > now, I would like to know for each element in V1 if it's equal to one of > the elements in V2 > I could do: > which(V1 == V2[1] | V1 == V2[2]) > > but what if I don't know the length of V2 and it's content???V1 %in% V2 [1] FALSE FALSE TRUE TRUE FALSE or is.element(V1, V2) [1] FALSE FALSE TRUE TRUE FALSE ?is.element> Thank you in advance! > > Antje > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Antje <niederlein-rstat at yahoo.de> writes:> Hello, > > I'm looking for a solution for the following problem: > I have two vectors > > V1 <- c("apple","honey","milk","bread","butter") > V2 <- c("bread","milk") > > now, I would like to know for each element in V1 if it's equal to one of > the elements in V2 > I could do: > which(V1 == V2[1] | V1 == V2[2]) > > but what if I don't know the length of V2 and it's content???%in%> Thank you in advance! > > Antje > > ______________________________________________ > 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. >-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
You could do this> V1 <- c("apple","honey","milk","bread","butter") > V2 <- c("bread","milk") > intersect(V1,V2)[1] "milk" "bread"> setdiff(V1,V2)[1] "apple" "honey" "butter"> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Antje > Sent: Wednesday, November 01, 2006 8:50 AM > To: R-help at stat.math.ethz.ch > Subject: [R] extract values from a vector > > Hello, > > I'm looking for a solution for the following problem: > I have two vectors > > V1 <- c("apple","honey","milk","bread","butter") > V2 <- c("bread","milk") > > now, I would like to know for each element in V1 if it's > equal to one of the elements in V2 I could do: > which(V1 == V2[1] | V1 == V2[2]) > > but what if I don't know the length of V2 and it's content??? > > Thank you in advance! > > Antje > > ______________________________________________ > 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. >
Am 1 Nov 2006 um 14:49 hat Antje geschrieben: Date sent: Wed, 01 Nov 2006 14:49:43 +0100 From: Antje <niederlein-rstat at yahoo.de> To: R-help at stat.math.ethz.ch Subject: [R] extract values from a vector> Hello, > > I'm looking for a solution for the following problem: > I have two vectors > > V1 <- c("apple","honey","milk","bread","butter") > V2 <- c("bread","milk") > > now, I would like to know for each element in V1 if it's equal to one > of the elements in V2 I could do: which(V1 == V2[1] | V1 == V2[2]) > > but what if I don't know the length of V2 and it's content??? >> V1%in%V2[1] FALSE FALSE TRUE TRUE FALSE Is this what you are looking for? HTH, Bernd