[R] counting the frequencies of a vector

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Oct 16 18:50:27 CEST 2008


2008/10/16 Jörg Groß <joerg at licht-malerei.de>:
> Hi,
>
>
> Is there a function which counts the frequencies of the occurence of a
> number within an interval?
>
> for example I have this vector:
>
> x <- c(1, 3, 1.2, 5, 5.9)
>
> and I want a vector that gives me the frequencies within an interval of 2,
> beginning at 0
> (so the intervals are 0-2, 2-4, 4-6 and so on)
>
> so I get these frequencies:
>
> 2, 1, 2
>
>
> Which functions do I have to use for this purpose?
>

 'cut' cuts your vector into chunks, and 'table' counts the number in
each chunk:

 > x
 [1] 1.0 3.0 1.2 5.0 5.9
 > table(cut(x,c(0,2,4,6)))

 (0,2] (2,4] (4,6]
     2     1     2

Barry



More information about the R-help mailing list