1

I have a vector, in this case a character vector. I want all the elements which only appear once in the vector, but the solution should be generalizable for limits other than 1.

I can pick them manually if I use the table function. I thought that the solution would look something like

frequencies <- table(myVector)
myVector[??@frequencies <= 1] 

But first of all, I don't know the slot name which will have to go into ??, and searches for documentation on the table object lead me to nowhere.

Second, while the documentation for table() says that it returns 'an object of class "table"', trying the above with some random word used instead of ??, I didn't get a "no such slot" error, but

Error: trying to get slot "frequencies" from an object of a basic class ("function") with no slots

which seems to indicate that the above won't function even if I knew the slot name.

So what is the correct solution, and how do I get at the separate columns in a table when I need them?

rumtscho
  • 2,474
  • 5
  • 28
  • 42
  • Your solution doesn’t work because the return value of `table` *has no slots*. Slots only exist in S4 objects and not many (as far as I know, none) of the R built-in functions work with S4. – Konrad Rudolph Dec 28 '13 at 10:35

2 Answers2

4

D'oh, the documentation of the table function led me on a merry chase of imaginary object slots.

Whatever the table() function returns, it acts as a simple numeric vector. So my solution idea works when written as:

threshold <- 1
frequencies <- table(myVector)
frequencies[frequencies <= threshold] 
rumtscho
  • 2,474
  • 5
  • 28
  • 42
2

You don’t need table for this:

vector <- c(1, 0, 2, 2, 3, 2, 1, 4)
threshold <- 1
Filter(function (elem) length(which(vector == elem)) <= threshold, vector)
# [1] 0 3 4

You can use table, but then you get the result as character strings rather than numbers. You can convert them back, of course, but it’s somehow less elegant:

tab <- table(vector)
names(tab)[tab <= threshold]
# [1] "0" "3" "4"
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • I found a simple solution myself too, but thank you for providing this one and for mentioning the part about S4 objects and built-in functions. – rumtscho Dec 28 '13 at 10:52