match()
function returns a vector of the positions of the matches of the first argument in the second argument.match(x, table, nomatch = NA_integer_, incomparables = NULL)
The parameters are:
-
x
:vector to be matched
-
table
: other vector the be matched against
-
nomatch
: value to show when no match is found
-
incomparables
: vector of values that can not be matchedx = c(10:20)
y = c(15:30)
x
## [1] 10 11 12 13 14 15 16 17 18 19 20
y
## [1] 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
match(x,y)
## [1] NA NA NA NA NA 1 2 3 4 5 6
The function returns a vector with a length equals to
x
, and each value in x
has to be match in a position in the vector y
. The function returns a vector of the positions of each value of x
in the vector y
. The vector shows 5 values of NA
since 10:15 are not found in y
, and then shows the values of the positions of the values 15:20 in y
.match(y,x)
## [1] 6 7 8 9 10 11 NA NA NA NA NA NA NA NA NA NA
It is showed a vector of the positions of each value of
y
in the vector x
. Only the first 6 values of y
are found in the vector x
, the numbers returned by the function are the positions of the values in the vector x
. For example, the first value in y
is match with the sixth value in x
.
Instead of returning
NA
when the values don’t match we can give a specific value for the values that have no match:match(x,y, nomatch = 0)
## [1] 0 0 0 0 0 1 2 3 4 5 6
Using the parameter
incomparables
we can give a vector of the values that we don’t want to be matched:x
## [1] 10 11 12 13 14 15 16 17 18 19 20
y
## [1] 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#if we don't want to match the values 15,16,17:
match(x,y, incomparables = c(15,16,17))
## [1] NA NA NA NA NA NA NA NA 4 5 6
#if we don't want to match the values 15,16,17:
match(x,y, nomatch = 0, incomparables = c(15,16,17))
## [1] 0 0 0 0 0 0 0 0 4 5 6
#if we don't want to match the values 18,19,20:
match(x,y, nomatch = 0, incomparables = c(18,19,20))
## [1] 0 0 0 0 0 1 2 3 0 0 0
Related to this functions we have the function:
%in%
, which instead of returning the positions that match the first with the second vector, returns a logical vector indicating if there is a match or not.match(x,y)
## [1] NA NA NA NA NA 1 2 3 4 5 6
x %in% y
## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
match(y,x)
## [1] 6 7 8 9 10 11 NA NA NA NA NA NA NA NA NA NA
y %in% x
## [1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE FALSE FALSE FALSE FALSE
No comments:
Post a Comment