1. 程式人生 > >Match function in R

Match function in R

nom -i strong UNC https tps true mat amp

Examples:


print(match(5, c(1,2,9,5,3,6,7,4,5)))
[1] 4


5 %in% c(1,2,9,5,3,6,7,4,5)
[1] TRUE


8 %in% c(1,2,9,5,3,6,7,4,5)
[1] FALSE

> v1 <- c("a1","b2","c1","d2")
> v2 <- c("g1","x2","d2","e2","f1","a1","c2","b2","a2")
> x <- match(v1,v2)
> x
[1] 6 8 NA 3


> v1 <- c("a1","b2","c1","d2")

> v2 <- c("g1","x2","d2","e2","f1","a1","c2","b2","a2")
> v1 %in% v2
[1] TRUE TRUE FALSE TRUE


> v1 <- c("a1","b2","c1","d2")
> v2 <- c("g1","x2","d2","e2","f1","a1","c2","b2","a2")
> x <- match(v1,v2, nomatch = 0)
> x
[1] 6 8 0 3


> v1 <- c("a1","b2","c1","d2")
> v2 <- c("g1","x2","d2","e2","f1","a1","c2","b2","a2")
> x <- match(v1,v2, nomatch = 0, incomparables = "a1")
> x
[1] 0 8 0 3

ref:

https://www.r-bloggers.com/match-function-in-r/

Match function in R