If I have a vector
vec = c('a','a','a','b','b','c','c','c','c','c')
Is there a simple way to find the least occurring element in vec
? Thanks!
Edit: is there a simple way to do it with characters?
If I have a vector
vec = c('a','a','a','b','b','c','c','c','c','c')
Is there a simple way to find the least occurring element in vec
? Thanks!
Edit: is there a simple way to do it with characters?
This should work, even if more than one of the elements is tied as the least frequent item:
vec = c(1,1,1,2,2,3,3,3,3,3)
f <- table(vec)
as.numeric(names(f[f == min(f)]))
# [1] 2
table(vec)[which.min(table(vec))]
(In all likelihood a duplicate, although I have searched. Found what seemed to be similar on the max side: Create a variable capturing the most frequent occurence by group Maybe it sounds familiar to that one 'cuz I posted an answer?)