确定向量中仅出现一次的元素索引

时间:2015-03-19 11:52:01

标签: r

我有一个矢量,例如像这样:

vec <- c(6, 6, 10, 13, 13, 15, 15, 15, 15, 16, 16, 19)

我现在想要只出现一次的元素的位置:

3, 12

唯一仅在已经发生的值时忽略这些值。

4 个答案:

答案 0 :(得分:9)

另一种选择:

which(!(vec %in% vec[duplicated(vec)]))
#[1]  3 12

答案 1 :(得分:3)

你可以试试这个

 which(vec %in%  names(table(vec))[table(vec)==1] )
# 3 12

答案 2 :(得分:3)

您也可以尝试ave,如下所示:

> which(as.logical(ave(vec, vec, FUN = function(x) length(x) == 1)))
[1]  3 12

由于ave似乎并不受欢迎,所以duplicated上有一个转折点:

which(!(duplicated(vec) | duplicated(vec, fromLast = TRUE)))

它表现得相当好......就像ave一样,顺便说一下: - )

## A bigger vector to hunt through
set.seed(1)
vec <- sample(c(1:9, sample(10:1000, 100000, TRUE)))

## Some functions to test
roland <- function() which(!(vec %in% vec[duplicated(vec)]))
am1 <- function() which(as.logical(ave(vec, vec, FUN = function(x) length(x) == 1)))
am2 <- function() which(!(duplicated(vec) | duplicated(vec, fromLast = TRUE)))
mb <- function() which(vec %in% names(table(vec))[table(vec)==1] )

## The benchmarks
library(microbenchmark)
microbenchmark(roland(), am1(), am2(), mb())
# Unit: milliseconds
#      expr        min         lq      mean     median         uq       max neval
#  roland()   6.869534   8.781927  13.83998   9.332151  10.577182  67.52081   100
#     am1()  15.778865  16.992881  23.74078  18.394768  20.341746  74.58536   100
#     am2()   4.764585   6.340731  11.20347   7.004970   7.492049  65.00799   100
#      mb() 117.185928 122.187247 132.90390 124.526029 127.875117 233.82788   100

答案 3 :(得分:2)

使用rle的另一种方法:

with(rle(sort(vec)), match(values[lengths==1], vec))
[1]  3 12