Splitting a continuous variable into equal sized groups

Or see cut_number from the ggplot2 package, e.g. das$wt_2 <- as.numeric(cut_number(das$wt,3)) Note that cut(…,3) divides the range of the original data into three ranges of equal lengths; it doesn’t necessarily result in the same number of observations per group if the data are unevenly distributed (you can replicate what cut_number does by using quantile appropriately, … Read more

Identify groups of continuous numbers in a list

EDIT 2: To answer the OP new requirement ranges = [] for key, group in groupby(enumerate(data), lambda (index, item): index – item): group = map(itemgetter(1), group) if len(group) > 1: ranges.append(xrange(group[0], group[-1])) else: ranges.append(group[0]) Output: [xrange(2, 5), xrange(12, 17), 20] You can replace xrange with range or any other custom class. Python docs have a … Read more