R 使用因素和水平对数据进行分组
我正在尝试制作一个频率表,将值分组到有限数量的容器中。
假设我有数据,
X <- c(1,2,3,4,3,9, 20)
我可以制作一个频率表,这样它就可以显示所有空单元格,如下所示:
(factor(X, levels = c(0:max(X))))
我不想显示每个可能值的频率,而是希望对值 >5
进行分箱,以便表格中的级别为:0、1、2、3、4、5 和 >5
。
我该怎么做?
I'm trying to make a frequency table that groups values into a limited number of bins.
Say I have the data
X <- c(1,2,3,4,3,9, 20)
I can make a frequency table such that it shows all the empty cells like this:
(factor(X, levels = c(0:max(X))))
Instead of showing the frequency of every possible value, I would like to bin values >5
so that the levels on the table are: 0, 1, 2, 3, 4, 5, and >5
.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您首先需要转换向量,使其具有唯一的条目,然后您可以在
factor()
函数中添加缺少的级别:这会导致:
You first need to transform the vector so that it has an unique entry for, then you can add the missing levels in the
factor()
function:This results in:
Sacha 已经给了您一个可行的答案,但为了将来的参考,您可能需要熟悉
cut
函数,该函数旨在将连续变量分解为块。Sacha has already given you a working answer, but for future reference, you may want to familiarise yourself with the
cut
function, which is designed to break up a continuous variable into chunks.