2017-02-14 27 views
2

我在努力弄清楚如何使用cut()函數通過12 months定義我感興趣的數據的interval。我看了這篇文章R - Cut by Defined Interval。但它無助於我所尋找的。通過cut()創建R的定義的間隔並生成直方圖

說,我有一組數據名months,它們的值小於year <12個月,直到50 months

set.seed(50); sample(50) -> months 

我想用cut()功能有哪些落在每年包括< 12 months數據的數量。

> cut(months, breaks = seq(12,50, by= 12))-> output 
> output 
[1] (24,36] (12,24] <NA> (36,48] (12,24] <NA> (24,36] (24,36] <NA> <NA> 
[11] (12,24] <NA> (24,36] (36,48] (36,48] (36,48] (24,36] (12,24] (36,48] <NA> 
[21] (12,24] (36,48] (12,24] (12,24] <NA> (12,24] (12,24] (24,36] <NA> <NA> 
[31] (12,24] (36,48] (24,36] (36,48] <NA> <NA> (36,48] (12,24] (36,48] (24,36] 
[41] (36,48] (12,24] (24,36] <NA> <NA> (24,36] <NA> (24,36] (24,36] (36,48] 
Levels: (12,24] (24,36] (36,48] 

> table(output) 
output 
(12,24] (24,36] (36,48] 
    12  12  12 

問題

我怎樣才能獲得的數據的數量< 12 months我一直有12個月interval

我試過了,但沒有奏效!

> cut(months, breaks = seq(1,12,50, by= 12))-> output 

2-我怎樣才能讓一個hist()陰謀,這個數據?

感謝,

+0

@GGamba,劑量並不完美:'輸出 (1,13](13,25](25,37](37,49] 12 12 12 12'我需要12不13 – Daniel

+2

! 'seq(0,50,by = 12)' – GGamba

+1

seq(0,50,by = 12)(抱歉與@Gamba同時發佈)另外,什麼是hiso()圖? –

回答

1
set.seed(50) 
months <- sample(50) 

output <- cut(months, breaks = seq(0,50, by= 12), labels = c("<12","12-24","24-35","36-50")) 

hist(as.numeric(output)) 

你必須手動編輯直方圖軸值,因爲他們將在間隔1-4標記。正如我在評論中提到的那樣。考慮到所有的值是相等的,直方圖不是非常有用。