Skip to content

frequency table with custom bands

5 messages · jcrosbie, arun, jim holtman

#
I would like to create a frequency table with custom bands. 

seq1 = seq(0, 100, by = 5)
seq2 = seq(100, 1000, by = 100)
Bands = c(seq1, seq2)
Prices = sample(1:1000, 200, replace=F)

How would  I go about find the frequency of prices within each band? 





--
View this message in context: http://r.789695.n4.nabble.com/frequency-table-with-custom-bands-tp4646413.html
Sent from the R help mailing list archive at Nabble.com.
#
HI,
May be this helps you:
res<-rbind(as.data.frame(table(cut(Prices,breaks=seq1))),as.data.frame(table(cut(Prices,breaks=seq2))))
res2<-apply(res,2,function(x) gsub("\\(|\\]","",gsub("[,]","-",x)))
res3<-within(as.data.frame(res2),{Freq<-as.numeric(Freq)})
?head(res3)
#?? Var1 Freq
#1?? 0-5??? 1
#2? 5-10??? 6
#3 10-15??? 2
#4 15-20??? 2
#5 20-25??? 6
#6 25-30??? 2
A.K.



----- Original Message -----
From: jcrosbie <james at crosb.ie>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, October 16, 2012 7:37 PM
Subject: [R] frequency table with custom bands

I would like to create a frequency table with custom bands. 

seq1 = seq(0, 100, by = 5)
seq2 = seq(100, 1000, by = 100)
Bands = c(seq1, seq2)
Prices = sample(1:1000, 200, replace=F)

How would? I go about find the frequency of prices within each band? 





--
View this message in context: http://r.789695.n4.nabble.com/frequency-table-with-custom-bands-tp4646413.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.
#
?cut

try this:
(0,5]      (5,10]     (10,15]     (15,20]     (20,25]
(25,30]     (30,35]     (35,40]
          1           2           1           1           0
2           1           1
    (40,45]     (45,50]     (50,55]     (55,60]     (60,65]
(65,70]     (70,75]     (75,80]
          1           1           1           1           1
1           1           0
    (80,85]     (85,90]     (90,95]    (95,100]   (100,200]
(200,300]   (300,400]   (400,500]
          1           2           0           1          22
20          17          22
  (500,600]   (600,700]   (700,800]   (800,900] (900,1e+03]
         25          20          16          18          20

        
On Tue, Oct 16, 2012 at 4:37 PM, jcrosbie <james at crosb.ie> wrote:

  
    
#
Hi Jim,

I am gettng the error message that the breaks are not unique: (R 2.15)
table(cut(Prices,breaks=Bands))
Error in cut.default(Prices, breaks = Bands) : 'breaks' are not unique


So, I tried using:
res<-rbind(as.data.frame(table(cut(Prices,breaks=seq1))),as.data.frame(table(cut(Prices,breaks=seq2))))
res2<-apply(res,2,function(x) gsub("\\(|\\]","",gsub("[,]","-",x)))
res3<-within(as.data.frame(res2),{Freq<-as.numeric(Freq)})
head(res3)
#?? Var1 Freq
#1?? 0-5??? 1
#2? 5-10??? 6
#3 10-15??? 2
#4 15-20??? 2
#5 20-25??? 6
#6 25-30??? 2


A.K.








----- Original Message -----
From: jim holtman <jholtman at gmail.com>
To: jcrosbie <james at crosb.ie>
Cc: r-help at r-project.org
Sent: Tuesday, October 16, 2012 8:52 PM
Subject: Re: [R] frequency table with custom bands

?cut

try this:
? ? ? (0,5]? ? ? (5,10]? ?  (10,15]? ?  (15,20]? ?  (20,25]
(25,30]? ?  (30,35]? ?  (35,40]
? ? ? ? ? 1? ? ? ? ?  2? ? ? ? ?  1? ? ? ? ?  1? ? ? ? ?  0
2? ? ? ? ?  1? ? ? ? ?  1
? ? (40,45]? ?  (45,50]? ?  (50,55]? ?  (55,60]? ?  (60,65]
(65,70]? ?  (70,75]? ?  (75,80]
? ? ? ? ? 1? ? ? ? ?  1? ? ? ? ?  1? ? ? ? ?  1? ? ? ? ?  1
1? ? ? ? ?  1? ? ? ? ?  0
? ? (80,85]? ?  (85,90]? ?  (90,95]? ? (95,100]?  (100,200]
(200,300]?  (300,400]?  (400,500]
? ? ? ? ? 1? ? ? ? ?  2? ? ? ? ?  0? ? ? ? ?  1? ? ? ? ? 22
20? ? ? ? ? 17? ? ? ? ? 22
? (500,600]?  (600,700]?  (700,800]?  (800,900] (900,1e+03]
? ? ? ?  25? ? ? ? ? 20? ? ? ? ? 16? ? ? ? ? 18? ? ? ? ? 20

        
On Tue, Oct 16, 2012 at 4:37 PM, jcrosbie <james at crosb.ie> wrote:

  
    
#
you can always use 'unique':

breaks <- unique(breaks)

or just make sure when you are defining your break sequence that they
are not overlapping as they were in your original data at 100.  That
is why in my example I was using:

        
On Tue, Oct 16, 2012 at 10:48 PM, arun <smartpink111 at yahoo.com> wrote: