2012-11-29 48 views
2

頻率表我是新來R.我要生成原始數據(小數)像一個頻率表:如何從原始數據中的R

x 
     V1 
1 10.10 
2 46.65 
3 53.60 
4 38.50 
5 45.95 
6 12.25 
7 59.60 
8 23.30 
9 11.05 
10 58.35 
11 40.20 
12 11.05 
13 10.45 
14 26.45 
15 13.25 
16 21.15 
17 35.00 
18 29.05 
19 25.40 
20 47.20 
21 42.45 
22 57.30 
23 55.65 
24 56.50 
25 26.95 
26 59.65 
27 32.10 
28 29.00 
29 34.75 
30 21.65 

弄成這個樣子:

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))) 

,但我得到的東西是這樣的:

錯誤cut.default(X,符= nclass.Sturges(X)): 'X' 必須是數字

我應該怎麼做 'X' 的數字?

按照要求:

ħ< - dput(X) 結構(列表(V1 = C(10.1,46.65,53.6,38.5,45.95,12.25, 59.6,23.3,11.05,58.35, 40.2,11.05,10.45,26.45,13.25,21.15, 35,29.05,25.4,47.2,42.45,57.3,55.65,56.5,26.95,59.65, 32.1,29,34.75,21.65)),.Names =「V1」 ,class =「data.frame」,row.names = c(NA, -30L))

+0

你可以做H的< - dput(X)並將h添加到您的帖子中? – agstudy

回答

7

x是數據幀。 x $ V1是數字。

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

+ 1爲我節省了一些鍵入 – mnel

+0

,因爲'cut()'已經產生了因子,是否真的有必要運行factor(cut())? –

3

如果你知道你正在使用你可以用histplot=FALSE

hist將返回直方圖類對象(h在下面的例子)什麼斷點。 h$counts爲您提供了由breaks參數定義的給定直方圖單元格的頻率。

> 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 

即使你不知道休息,你可以使用histplot=FALSE並取得像樣的成績爲默認休息是「斯特奇斯」

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