使用向量中的x值计算元素的数量 [英] Counting the number of elements with the values of x in a vector
本文介绍了使用向量中的x值计算元素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数字向量:
数字< - c(4,23,4,23,5, 43,44,56,657,67,67,435,
453,435,324,34,456,56,567,65,34,435)
解决方案
您可以使用 table()
:
> a< - table(numbers)
> a
数字
4 5 23 34 43 54 56 65 67 324 435 453 456 567 657
2 1 2 2 1 1 2 1 2 1 3 1 1 1 1
然后您可以将其子集:
> a [names(a)== 435]
435
3
如果你更舒适的工作,将它转换成一个data.frame:
> as.data.frame(表(数字))
数字频率
1 4 2
2 5 1
3 23 2
4 34 2
。 ..
I have a vector of numbers:
numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,
453,435,324,34,456,56,567,65,34,435)
How can I have R count the number of times a value x appears in the vector?
解决方案
You can just use table()
:
> a <- table(numbers)
> a
numbers
4 5 23 34 43 54 56 65 67 324 435 453 456 567 657
2 1 2 2 1 1 2 1 2 1 3 1 1 1 1
Then you can subset it:
> a[names(a)==435]
435
3
Or convert it into a data.frame if you're more comfortable working with that:
> as.data.frame(table(numbers))
numbers Freq
1 4 2
2 5 1
3 23 2
4 34 2
...
这篇关于使用向量中的x值计算元素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文