[R] little question

Roger Bivand rsb at reclus.nhh.no
Thu Apr 26 20:07:41 CEST 2001


On Thu, 26 Apr 2001, Olivier Martin wrote:

> i have two vectors x and yn and they don t have the same length...
> (i suppose length(x)>length(y))
> this is what i want to do.
> i suppose that
> x=c(10,1,2,3,7,9,1,4,2,11,2,10) and y=c(1,2,4,10)
> i want to find all the numbers of rows i of x for which x[i] exists in y
> ...
>  i.e. in this case ...rep=(1,2,3,7,8,9,11,12)
> 
Something like:
> x <- c(10,1,2,3,7,9,1,4,2,11,2,10)
> y <- c(1,2,4,10)
> x %in% y
 [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE
TRUE
> which(x %in% y)
[1]  1  2  3  7  8  9 11 12

You could do it with match() too.

Roger

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no
and: Department of Geography and Regional Development, University of
Gdansk, al. Mar. J. Pilsudskiego 46, PL-81 378 Gdynia, Poland.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list