获取R中向量中重复值的第一个元素的索引

Rez*_*eza 1 r vector subset

我们如何才能a获得重复值的第一个元素的索引(first 1,first 2,first 3...的索引)?

a <- c(rep(1, 3), rep(2, 2), rep(3, 1), rep(4, 2))

desired.output <- c(1, 4, 6, 7)
Run Code Online (Sandbox Code Playgroud)

mar*_*kus 5

另一种选择是 match

match(unique(a), a)
# [1] 1 4 6 7
Run Code Online (Sandbox Code Playgroud)

help('match')

match返回其第二个参数的第一个(第一个)匹配位置的向量。