如何从R中的原始数据生成频率表

如何从R中的原始数据生成频率表,r,frequency,R,Frequency,我是R新手。我想从原始数据(小数)生成一个频率表,如: 变成这样: Class Frequency (10.00 - 19.99) 6 (20.00 - 29.99) 8 (30.00 - 39.99) 4 (40.00 - 49.99) 5 (50.00 - 59.99) 7 我使用以下代码: factorx<-factor(cut(x, breaks=nclass.Sturges(x

我是R新手。我想从原始数据(小数)生成一个频率表,如:

变成这样:

Class            Frequency
(10.00 - 19.99)         6
(20.00 - 29.99)         8
(30.00 - 39.99)         4
(40.00 - 49.99)         5
(50.00 - 59.99)         7
我使用以下代码:

factorx<-factor(cut(x, breaks=nclass.Sturges(x)))

factorxx是一个数据帧。x$V1是数字

factor(cut(x$V1, breaks=nclass.Sturges(x$V1)))

如果您知道正在使用的断点,则可以将
hist
plot=FALSE

hist
将返回直方图类对象(
h
在下面的示例中)
h$counts
提供由
中断
参数定义的给定直方图单元格的频率

> x
 [1] 10.10 46.65 53.60 38.50 45.95 12.25 59.60 23.30 11.05 58.35 40.20 11.05 10.45 26.45 13.25 21.15 35.00 29.05 25.40 47.20
[21] 42.45 57.30 55.65 56.50 26.95 59.65 32.10 29.00 34.75 21.65
> h <- hist(x, plot=FALSE, breaks = c(10,20,30,40,50,60))
> h
$breaks
[1] 10 20 30 40 50 60

$counts
[1] 6 8 4 5 7

$intensities
[1] 0.02000000 0.02666667 0.01333333 0.01666667 0.02333333

$density
[1] 0.02000000 0.02666667 0.01333333 0.01666667 0.02333333

$mids
[1] 15 25 35 45 55

$xname
[1] "x"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"
> h$counts 
[1] 6 8 4 5 7
既然
cut()
已经产生了因子,你能做h吗?真的有必要运行
factor(cut())
> x
 [1] 10.10 46.65 53.60 38.50 45.95 12.25 59.60 23.30 11.05 58.35 40.20 11.05 10.45 26.45 13.25 21.15 35.00 29.05 25.40 47.20
[21] 42.45 57.30 55.65 56.50 26.95 59.65 32.10 29.00 34.75 21.65
> h <- hist(x, plot=FALSE, breaks = c(10,20,30,40,50,60))
> h
$breaks
[1] 10 20 30 40 50 60

$counts
[1] 6 8 4 5 7

$intensities
[1] 0.02000000 0.02666667 0.01333333 0.01666667 0.02333333

$density
[1] 0.02000000 0.02666667 0.01333333 0.01666667 0.02333333

$mids
[1] 15 25 35 45 55

$xname
[1] "x"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"
> h$counts 
[1] 6 8 4 5 7
> h2 <- hist(x, plot=FALSE)
> h2$breaks
[1] 10 20 30 40 50 60
> h2$counts
[1] 6 8 4 5 7