「R で特定の値の行や列を見つける方法」で,match 関数が挙げられている。後半に which.max, which.min が挙げられているのになぜ,which に言及されていないのか?
まず,ベンチマークテスト
> a <- 1:1e6
> library(rbenchmark)
> benchmark(which(a == 1e4), match(1e4, a), columns=c("test", "elapsed", "relative", "user.self", "sys.self", "replications"))
test elapsed relative user.self sys.self replications
2 match(10000, a) 7.227 7.796117 6.921 0.364 100
1 which(a == 10000) 0.927 1.000000 0.729 0.209 100
match と which の違いを示す
> x <- c(9, 5, 10, 7, 4, 2, 6, 8, 3, 4)
> match(4, x)
[1] 5
> which(x == 4)
[1] 5 10
> match(c(5, 10), x)
[1] 2 3