In R, how do I create binned factor variables from a continuous variable, with custom breaks?


I have a vector that looks like this:


I would like to create four dummy categories, in which I bin the continuous dataset by custom breaks . .. for example: 1:4, 5:9, 10:17, 18:23.

The output dummy categories would have the same length as the original continuous vector (18 in this case), but now each binned dummy variable would just contain a 1 or a 0.

Use cut:

data.frame(dataset, bin=cut(dataset, c(1,4,9,17,23), include.lowest=TRUE))