を参照してください?cut
。次に例を示します。
set.seed(42)
dat <- data.frame(Values = sample.int(50000, size = 100))
## create factor indicating which categoriesy data are in
grps <- with(dat, cut(Values, breaks = c(1,3,6,12,30,50000)))
これは与える:
> head(grps)
[1] (30,5e+04] (30,5e+04] (30,5e+04] (30,5e+04] (30,5e+04] (30,5e+04]
Levels: (1,3] (3,6] (6,12] (12,30] (30,5e+04]
> table(grps)
grps
(1,3] (3,6] (6,12] (12,30] (30,5e+04]
0 0 1 0 99
データフレームでそれが必要な場合は、代わりにこれを試してください。
dat2 <- within(dat, Groups <- cut(Values, breaks = c(1,3,6,12,30,50000)))
その結果
> head(dat2)
Values Groups
1 45741 (30,5e+04]
2 46853 (30,5e+04]
3 14307 (30,5e+04]
4 41520 (30,5e+04]
5 32085 (30,5e+04]
6 25953 (30,5e+04]
異なるラベルを付けたい場合は、結果の因子のレベルを変更できます。