How to find all values which only appear less than X times in a vector

∥☆過路亽.° 提交于 2019-12-11 10:34:01

问题


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?


回答1:


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"



回答2:


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] 


来源:https://stackoverflow.com/questions/20813416/how-to-find-all-values-which-only-appear-less-than-x-times-in-a-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!