I'm quite new to R. I just working on a problem.
我对r很陌生,我只是在研究一个问题。
I'm trying to make a frequency table.
我想做一个频率表。
Say I have the data
假设我有数据
X<-c(1,2,3,4,3,9, 20)
and I want to make a frequency table such that it shows all the empty cells
我想做一个频率表它显示所有的空单元格
(factor(X, levels = c(0:max(X))))
Now what I want I have trying to do with R, is make it group the it so the levels, are in fact, 0, 1,2,3,4,5, and >5.
现在我要对R做的是,把它归到它的组中所以水平,实际上是,0 1 2 3 4 5 >5。
Any advice on this would be great
对此有任何建议都很好
2 个解决方案
#1
3
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:
首先需要对向量进行变换,使其具有惟一的条目,然后可以在factor()函数中添加缺失的级别:
X <- c(1,2,3,4,3,9,20)
X <- ifelse(X>5,">5",X)
X <- factor(X,levels=c(0:5,">5"))
This results in:
这将导致:
X [1] 1 2 3 4 3 >5 >5 Levels: 0 1 2 3 4 5 >5
X[1] 1 2 3 4 3 3 >5 >5水平:0 1 2 3 4 5 >5
#2
1
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.
Sacha已经给出了一个有效的答案,但为了以后的参考,您可能需要熟悉cut函数,它旨在将连续变量分解为块。
x <- cut(x, c(-Inf, 0:5, Inf), labels=c(0:5, ">5"))
#1
3
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:
首先需要对向量进行变换,使其具有惟一的条目,然后可以在factor()函数中添加缺失的级别:
X <- c(1,2,3,4,3,9,20)
X <- ifelse(X>5,">5",X)
X <- factor(X,levels=c(0:5,">5"))
This results in:
这将导致:
X [1] 1 2 3 4 3 >5 >5 Levels: 0 1 2 3 4 5 >5
X[1] 1 2 3 4 3 3 >5 >5水平:0 1 2 3 4 5 >5
#2
1
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.
Sacha已经给出了一个有效的答案,但为了以后的参考,您可能需要熟悉cut函数,它旨在将连续变量分解为块。
x <- cut(x, c(-Inf, 0:5, Inf), labels=c(0:5, ">5"))