# [R] Counting frequency within each range

arun smartpink111 at yahoo.com
Fri Jul 27 04:57:59 CEST 2012

```Hi,

Try this:a<-c(4,5,23,34,43,54,56,65,67,324,435,453,456,567,657)
a1<-melt(table(cut(a,breaks=c(0,10,20,30,60,120,240,480,960))))
a2<-data.frame(sapply(a1,function(x) gsub("\\(|\\]","",gsub("\\,","-",x))))
colnames(a2)<-c("numbers","Freq")
a2
#  numbers Freq
#1    0-10    2
#2   10-20    0
#3   20-30    1
#4   30-60    4
#5  60-120    2
#6 120-240    0
#7 240-480    4
#8 480-960    2

#You can also get the same result by:
a1<-as.data.frame(table(cut(a,breaks=c(0,10,20,30,60,120,240,480,960),labels=c("0-10","10-20","20-30","30-60","60-120","120-240","240-480","480-960"))) )
colnames(a1)<-c("numbers","Freq")
a1

A.K.

----- Original Message -----
From: Chintanu <chintanu at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Thursday, July 26, 2012 9:22 PM
Subject: [R] Counting frequency within each range

Hi,

This is a simple problem, but for the life of me I cannot find the answer.
How to determine frequency within given ranges ?
I know that table() gives frequency, for example

a <- table(numbers)
> a
numbers
4   5  23  34  43  54  56  65  67 324 435 453 456 567 657
2   1   2   2     1   1     2   1     2     1     3     1       1   1   1

> as.data.frame(table(numbers))
numbers Freq
1        4    2
2        5    1
3       23    2
4       34    2
...

However, there must be some simple way to get the frequency based on
ranges, such as 0-10, 10-20, 20-30 and so on.

Thank you.

Cheers,
Chintanu

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help