I have a dataset which looks like this:
ID a b
ID1 0.1 20.3
ID2 0.2 21.6
ID3 1.2 1.5
etc.
I would like to group these values into equal-sized groups. This can be done with:
data$bin1 <- as.numeric(cut2(data$b,g=50))
This takes the values in column b and divides them into 50 equal-sized groups:
ID a b bin
ID1 0.1 20.3 2
ID2 0.2 21.6 2
ID3 1.2 1.5 1
etc.
However, I need to do this multiple times, with different amount of groups. I tried:
for (i in 1:5){
data$bin[i] <- as.numeric(cut2(data$values,g=i*50))
}
But then I get this warning: "number of items to replace is not a multiple of replacement length".
After grouping the values, I want to calculate the means within each group, which can be done with:
means <- ddply(data,.(bin),summarise,mean.a=mean(a),mean.b=mean(b))
I would like to do this for all the bin sizes. Do I need to use another for loop? Or can it be implemented in the first loop?