# [R] Binning question (binning rows of a data.frame according to a variable)

Dan Bolser dmb at mrc-dunn.cam.ac.uk
Sun Mar 19 19:58:15 CET 2006

```Adaikalavan Ramasamy wrote:
> Do you by any chance want to sample from each group equally to get an
> equal representation matrix ?

No.

I want to make groups of equal sizes, where size isn't simply number of
rows (allowing a simple 'gl'), but a sum of the variable.

Thanks for the code though, it looks useful.

Here is an analogy for what I want to do (in case it helps).

A group of students have some bags of marbles - The marbles have
different colours. Each student has one bag, but can have between 5 and
50 marbles per bag with any given strange distribution you like. I line
the students up by age, and want to see if there is any systematic
difference between the number of each color of marble by age (older
students may find primary colours less 'cool').

Because the statistics of each individual student are bad (like the
proportion of each color per student -- has a high variance) I first put
all the students into 8 groups (for example).

Thing is, for one reason or another, the number of marbles per bag may
systematically vary with age too. However, I am not interested in the
number of marbles per bag, so I would like to group the students into 8
groups such that each group has the same total number of marbles. (Each
group having a different sized age range, none the less ordered by age).

Then I can look at the proportion (or count) of colours in each group,
and I can compare the groups or any trend accross the groups.

Does that make sense?

Cheers,
Dan.

> Here is an example of the input :
>
>  mydf <- data.frame( value=1:100, value2=rnorm(100),
>                      grp=rep( LETTERS[1:4], c(35, 15, 30, 20) ) )
>
> which has 35 observations from A, 15 from B, 30 from C and 20 from D.
>
>
> And here is a function that I wrote:
>
>  sample.by.group <- function(df, grp, k, replace=FALSE){
>
>    if(length(k)==1){ k <- rep(k, length(unique(grp))) }
>
>    if(!replace && any(k > table(grp)))
>      stop( paste("Cannot take a sample larger than the population when
>      'replace = FALSE'.\n", "Please specify a value greater than",
>      min(table(grp)), "or use 'replace = TRUE'.\n") )
>
>
>    ind   <- model.matrix( ~ -1 + grp )
>    w.mat <- list(NULL)
>
>    for(i in 1:ncol(ind)){
>      w.mat[[i]] <- sample( which( ind[,i]==1 ), k[i], replace=replace )
>    }
>
>    out <- df[ unlist(w.mat), ]
>    return(out)
>  }
>
>
> And here are some examples of how to use it :
>
> mydf <- mydf[ sample(1:nrow(mydf)), ]   # scramble it for fun
>
>
> out1 <- sample.by.group(mydf, mydf\$grp, k=10 )
> table( out1\$grp )
>
>  out2 <- sample.by.group(mydf, mydf\$grp, k=50, replace=T) # ie bootstrap
>  table( out2\$grp )
>
> and you can even do bootstrapping or sampling with weights via:
>
>  out3 <- sample.by.group(mydf, mydf\$grp, k=c(20, 20, 30, 30), replace=T)
>  table( out3\$grp )
>
>
>
>
>
> On Fri, 2006-03-17 at 16:01 +0000, Dan Bolser wrote:
>
>>Hi,
>>
>>I have tuples of data in rows of a data.frame, each column is a variable
>>for the 'items' (one per row).
>>
>>One of the variables is the 'size' of the item (row).
>>
>>I would like to cut my data.frame into groups such that each group has
>>the same *total size*. So, assuming that we order by size, some groups
>>should have several small items while other groups have a few large
>>items. All the groups should have approximately the same total size.
>>
>>I have tried various combinations of cut, quantile, and ecdf, and I just
>>can't work out how to do this!
>>
>>Any help is greatly appreciated!
>>
>>All the best,
>>Dan.
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help