match(x, y)
Identify where the elements of x are located in y.
- x – A vector.
- y – A vector.
Example. Vectors x and y are initialized, then we look for where the elements of xmatch those of y. Notice that only the first occurrence of each element of x is reported. Additionally, since 80 is not found, NA is returned for its location.
> set.seed(5)
> x <- c(1, 4, 5, 80)
> y <- c(2, 9, 8, 4, sample(15), 1)
> x
[1] 1 4 5 80
>
> y
[1] 2 9 8 4 4 10 12 15 2 8 5 7 14 1 11 9
[17] 6 13 3 1
>
> match(x,y)
[1] 14 4 11 NA
> match(a,b,nomatch = -1)
[1] -1 1 -1 -1 4 5 -1 -1 1
No comments:
Post a Comment