Getting values ​​that are displayed exactly n times

I specifically began to think in this problem, trying to get values ​​from a vector that was not repeated. uniquenot suitable (to the point that I could get from the documentation), because it gives you duplicate elements, but only once. duplicatedhas the same problem as it gives you FALSE on the first detection of a value that is duplicated. This was my workaround.

> d=c(1,2,4,3,4,6,7,8,5,10,3)
> setdiff(d,unique(d[duplicated(d)]))
[1]  1  2  6  7  8  5 10

Below is a more general approach

> table(d)->g
> as.numeric(names(g[g==1]))
[1]  1  2  5  6  7  8 10

which we can generalize to a different value than 1. But I find this solution a little awkward, turning strings into numbers. Is there a better or easier way to get this vector?

+4
source share
5 answers

, rle , n .

rl <- rle(sort(d))

rl$values[rl$lengths==1]
## [1]  1  2  5  6  7  8 10

rl$values[rl$lengths==2]
## [1] 3 4
+4

,

, , 2 . plyr:

count(d)$x[count(d)$freq==2]
#Output
#[1] 3 4
+3

duplicated n = 1, fromLast.

sort(d[! (duplicated(d) | duplicated(d, fromLast=TRUE))])
# [1]  1  2  5  6  7  8 10
+2

- R.

as.numeric(levels(factor(d))[tabulate(factor(d)) == 1])
# [1]  1  2  5  6  7  8 10

factor levels, ( "d" 0s).


Of course, even for something like this, you can expect a performance improvement from "data.table" with which you can do something like:

library(data.table)
as.data.table(d)[, .N, by = d][N == 1]$d
# [1]  1  2  6  7  8  5 10
+2
source

I prefer other answers, but this seemed like a good excuse to test my skills with dplyr:

library(dplyr)
as.data.frame(table(d)) %>%
  filter(Freq == 1) %>%
  select(d)
---
   d
1  1
2  2
3  5
4  6
5  7
6  8
7 10
+1
source

All Articles