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
- cut2 {Library Hmisc} Although it looks like the cut()
functions with additional useful arguments, it differs, e.g. it does not have a label=
argument (generates default labels using the lower and upper boundary of the bin).
cut2(urb,g=3) # 3 groups based on quantiles
cut2(urb,g=3,levels.mean=T) # same but labels are the average, instead of bound pairs
cut2(urb, c(25,50,70)) # user defined border limits
cut2(urb,m=10) # m=desired minimum of cases
- recode(){Package car}
- recodevar(){Package doBy}
Related commands