# [R] subtotals based on price bands?

arun smartpink111 at yahoo.com
Wed Oct 17 23:42:29 CEST 2012

```
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

________________________________
From: arun kirshna [via R] <ml-node+s789695n4646484h81 at n4.nabble.com>
To: jcrosbie <james at crosb.ie>
Sent: Wednesday, October 17, 2012 10:32 AM
Subject: Re: subtotals based on price bands?

Hi,
May be this helps:
seq1 = seq(0, 100, by = 5)
seq2 = seq(100, 1000, by = 100)
Bands = c(seq1, seq2)
set.seed(1)
Prices = sample(1:1000, 200, replace=F)
set.seed(1)
size = sample(1:1000, 200, replace=F)
Prices1<-cut(Prices,breaks=unique(Bands))
tapply(size,Prices1,sum)
#      (0,5]      (5,10]     (10,15]     (15,20]     (20,25]     (25,30]
#       NA          NA          26          NA          23          NA
#(30,35]     (35,40]     (40,45]     (45,50]     (50,55]     (55,60]
#   31          NA          NA          NA          54          NA
#(60,65]     (65,70]     (70,75]     (75,80]     (80,85]     (85,90]
#  126          67          NA          79          82         179
#(90,95]    (95,100]   (100,200]   (200,300]   (300,400]   (400,500]
#  186          NA        2627        5342        8927        9961
#(500,600]   (600,700]   (700,800]   (800,900] (900,1e+03]
#  10932       14913       18677       12833       16022
table(Prices1)
#Prices1
#   (0,5]      (5,10]     (10,15]     (15,20]     (20,25]     (25,30]
#     0           0           2           0           1           0
#(30,35]     (35,40]     (40,45]     (45,50]     (50,55]     (55,60]
#    1           0           0           0           1           0
#(60,65]     (65,70]     (70,75]     (75,80]     (80,85]     (85,90]
#    2           1           0           1           1           2
#(90,95]    (95,100]   (100,200]   (200,300]   (300,400]   (400,500]
#    2           0          17          22          25          22
#(500,600]   (600,700]   (700,800]   (800,900] (900,1e+03]
#     20          23          25          15          17
A.K.

________________________________

If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/subtotals-based-on-price-bands-tp4646473p4646484.html
This email was sent by arun kirshna (via Nabble)
To receive all replies by email, subscribe to this discussion

--
View this message in context: http://r.789695.n4.nabble.com/subtotals-based-on-price-bands-tp4646473p4646505.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

```