[英]How to find all values which only appear less than X times in a vector
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.
我希望所有元素仅在向量中出现一次,但是对于1以外的限制,解决方案应该可以推广。
I can pick them manually if I use the table
function. 如果使用
table
功能,我可以手动选择它们。 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. 但是首先,我不知道插槽名称必须输入??,并且在
table
对象上搜索文档导致我无处可去。
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 其次,虽然table()的文档说它返回“类为“ table”的对象”,并尝试使用一些随机词代替??来进行上述操作,但我没有遇到“ no such slot”错误,但是
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? 那么,什么是正确的解决方案,以及如何在我的单独列得到
table
,当我需要他们?
D'oh, the documentation of the table function led me on a merry chase of imaginary object slots. D'oh,表函数的文档使我对虚构的对象插槽进行了追逐。
Whatever the table() function returns, it acts as a simple numeric vector. 无论table()函数返回什么,它都充当简单的数值向量。 So my solution idea works when written as:
所以我的解决方案的想法写成:
threshold <- 1
frequencies <- table(myVector)
frequencies[frequencies <= threshold]
You don't need table
for this: 您不需要此
table
:
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. 您可以使用
table
,但是随后将结果作为字符串而不是数字来获取。 You can convert them back, of course, but it's somehow less elegant: 您当然可以将它们转换回去,但是它不那么优雅:
tab <- table(vector)
names(tab)[tab <= threshold]
# [1] "0" "3" "4"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.