Now that we know more about factors, cut()
will make more sense:
x = 1:100
cx = cut(x, breaks=c(0,10,25,50,100))
head(cx)
## [1] (0,10] (0,10] (0,10] (0,10] (0,10] (0,10]
## Levels: (0,10] (10,25] (25,50] (50,100]
table(cx)
## cx
## (0,10] (10,25] (25,50] (50,100]
## 10 15 25 50
We can also leave off the labels
cx = cut(x, breaks=c(0,10,25,50,100), labels=FALSE)
head(cx)
## [1] 1 1 1 1 1 1
table(cx)
## cx
## 1 2 3 4
## 10 15 25 50
Note that you have to specify the endpoints of the data, otherwise some of the categories will not be created
cx = cut(x, breaks=c(10,25,50), labels=FALSE)
head(cx)
## [1] NA NA NA NA NA NA
table(cx)
## cx
## 1 2
## 15 25
table(cx,useNA="ifany")
## cx
## 1 2 <NA>
## 15 25 60