我们如何才能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)
另一种选择是 match
match(unique(a), a)
# [1] 1 4 6 7
Run Code Online (Sandbox Code Playgroud)
从 help('match')
match返回其第二个参数的第一个(第一个)匹配位置的向量。
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |