我正在努力弄清楚如何使用cut()
函数来interval
定义我感兴趣的数据12 months
。我读过这篇文章R - Cut by Defined Interval。但它无助于我所寻找的东西。
说,我有一组数据名称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
问题
1 - 如何获取< 12 months
的数据数量我保持12个月interval
?
我尝试了这个但是不起作用!
> cut(months, breaks = seq(1,12,50, by= 12))-> output
2 - 如何通过此数据制作hist()
图?
谢谢,
答案 0 :(得分:2)
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的间隔进行标记。正如我在评论中提到的那样。考虑到所有的值都相等,直方图并不是非常有用。
答案 1 :(得分:0)