This does literally what you asked for

> x[x %in% y]
[1] 2 3

This is more likely what you want
> which(x %in% y)
[1] 2 3
>
It is an artifact of this example that the two results look the same.  Here
is a different example
that distinguishes them, and in this example, %in% is probably not right
either.  match() would be better.
> x <- sample(100, 11)
> x
 [1] 99 76 97 65 20 70 32  6 80 16 51
> y <- c(65, 99)
> y
[1] 65 99
> x %in% y
 [1]  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> x[x %in% y]
[1] 99 65
> which(x %in% y)
[1] 1 4
> mm <- match(y, x, 0)
> mm
[1] 4 1
> x[mm]
[1] 65 99
>
We care because this is only useful if there is more than one column.

> xx <- cbind(x, 11:21)
> xx[mm,]
      x
[1,] 65 14
[2,] 99 11
>
> ## compare to
> xx[which(x %in% y),] ## same rows but in wrong order
      x
[1,] 99 11
[2,] 65 14
>

Rich


2012/1/8 Philipp Chapkovski <chapkov...@gmail.com>

> x<-1:11
> y<-2:3
>
> is there a way to do something like
> x[x==y]
> (which actually produce error)?
> I am really got stuck
>
> ______________________________________________
> 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]]

______________________________________________
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.

Reply via email to