Create groups from continuous variables

Quite often you will need to create groups (bins, categories) from continuous variables, for instance to compare several levels of Urbanization (e.g. "High","average","Low") using strip boxplots.

Function cut()
cut(urb,3) three groups (same bin width)
cut(urb,c(25,50,70)) Specify cut points
cut(urb,3,labels=c("low","middle","high")) add labels for each bin (default upper/lower bound)
cut(urb, quantile(urb, c(0, 1/3, 2/3, 1))) specify cut points based on quantiles

To add or change labels you can use the level function:

levels(urb)[1] <- "low"Label the first element
levels(urb) <- c("Low","middle","high")Label the first three elements
levels(urb) displays the currently defined labels
ifelse
urbcat <- ifelse(urb > median(urb),c("Low urb"), c("High urb.")) Two groups based urbanisation below and above the median, with labels
world$urbcat1[urb > 80] <- "Very high urb"
world$urbcat1[urb > 40 & urb <= 80] <- "Moderate urb."
world$urbcat1[urb <= 40] <- "Low urb."
Other functions creating groups
Related commands