找到前三个最大元素及其向量索引

The*_*nse 1 r vector

我有一个看起来像这样的矢量

[12,3,4,5,6,7,8,12]
Run Code Online (Sandbox Code Playgroud)

我想找到索引和前三个最大数字的值.最大数字也可以像上面的矢量12重复那样重复.

我用了

哪一个

但它只返回一个数字的索引如何才能完成

输出

[12,12,8]
[1,8,7]
Run Code Online (Sandbox Code Playgroud)

我也读过这个Stack Overflow,但它没有帮助

小智 5

x <- c(12, 3, 4, 5, 6, 7, 8, 12)
sort.int(x, decreasing = TRUE, index.return = TRUE)
# $x
# [1] 12 12  8  7  6  5  4  3

# $ix
# [1] 1 8 7 6 5 4 3 2
Run Code Online (Sandbox Code Playgroud)

然后,前三个元素:

sort.int(x, decreasing = TRUE, index.return = TRUE)$ix[1:3]
# [1] 1 8 7
Run Code Online (Sandbox Code Playgroud)