Skip to content
Prev 308258 / 398503 Next

subtotals based on price bands?

HI,
Try this:
seq1 = seq(0, 100, by = 5) 
seq2 = seq(100, 1000, by = 100) 
Bands = c(seq1, seq2) 

DF1 <- data.frame(matrix(ncol=2,nrow=200)) 
colnames(DF1)<- c("Price", "Size") 
set.seed(1) 
DF1$Price <- sample(1:1000, 200, replace=F) 
set.seed(300) 
DF1$Size <- sample(1:1000, 200, replace=F) 
DF1$Price1<-cut(DF1$Price,breaks=unique(Bands)) 

res<-aggregate(DF1$Size,by=list(DF1$Price1),sum) 
#or
res2<-data.frame(sum=tapply(DF1$Size,DF1$Price1,sum)) 
#or
library(plyr)
res3<-ddply(DF1,.(Price1),summarize, sum=sum(Size))
A.K.

----- Original Message -----
From: jcrosbie <james at crosb.ie>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, October 17, 2012 3:37 PM
Subject: Re: [R] subtotals based on price bands?

Thank you, but I believe that's not returning what I want.?

This maybe a better dat aset to work with.?

seq1 = seq(0, 100, by = 5)?
seq2 = seq(100, 1000, by = 100)?
Bands = c(seq1, seq2)?


DF1 <- data.frame(matrix(ncol = 2, 200))
colnames(DF1)<- c("Price", "Size")
DF1$Price <_ sample(1:1000, 200, replace=F)?
DF1$Size <_ sample(1:1000, 200, replace=F)

I'm looking to find the subtotal of size when their price falls within a band.?

The bands go 0 to 5, 5 to 10,....100 to 200,..., 900 to 1000.?

Therefore if we had:
Price Size
210
220
1120
1120

The total in this case would be
0 to 5?30
10 to 2040

Thank you