[R] extract values from a vector

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Nov 1 14:58:18 CET 2006


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



More information about the R-help mailing list