简体   繁体   English

如何查找向量中出现少于X次的所有值

[英]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.

相关问题 R 查找向量 x 中大于向量 y 中的值的计数 - R find count of values in vector x which are greater than values in vector y 找到向量中的最大元素小于R中另一个向量中的值 - Find the largest element in a vector less than values in another vector in R 给定一个向量a = [1,2,3.2,4,5]和一个元素x = 3在向量a中,如何找到大于x的确切条目? - Given a vector a=[1,2, 3.2, 4, 5] and an element x=3 In vector a, how to find the exact entry which is bigger than x? 对一个向量中小于另一个向量的所有值的差求和 - Summing the difference of all values of one vector that are less than the values in another 替换列中出现少于x次的值 - Replacing values in column that occur less than x times 在R中,如何仅在连续出现超过x次的情况下才能检测出数据列中值大于y的连续数据点? - In R, how can one detect consecutive data points within a column of data which have a value >y only when they appear more than x times consecutively? 如何将嵌套df中变量中出现次数少于k次的所有值设置为0 - How to set to 0 all values that appeares less than k times in variables within nested df 当向量中的所有值均小于10 ^ -8时要求代码停止 - Asking the code to stop when all values in a vector are less than 10^-8 R 在 dataframe 中找到小于另一个向量中的值的值 - R find values in a dataframe which are smaller than values in another vector 计算小于x的值,并通过多个组找到x的最接近的值 - Count values less than x and find nearest values to x by multiple groups
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM