[R] group values in classes

Neal H. Walfield neal at walfield.org
Mon Dec 31 12:55:01 CET 2012


At Mon, 31 Dec 2012 12:13:43 +0200,
catalin roibu wrote:
> 
> Dear R users,
> I want to group numerical values in classes with different size and count
> the values for each classes.
> 
> My data is in this forma:
>  d  15  12,5  30,4  20,5  80,4  100,5  8,2  40,5  33  21  11
> And I want the group them in classes with 4 (5,....etc) cm size like this:
>  class d  16  16  32  24  84  104  12  44  36  24  12
> and final to count values for each class:
> class d                               n  12 2  16 2  24 2  32 1  36 1  44 1
> 84 1  104 1  Total 11

I rounded the sizes to the nearst whole cm.  It's not clear to me how
you want to group the elements from your description...

> data = c(15,  12.5,  30.4,  20.5,  80.4,  100.5,  8.2,  40.5,  33, 21,  11)
> aggregate(data, list(round(data)), length)
   Group.1 x
1        8 1
2       11 1
3       12 1
4       15 1
5       20 1
6       21 1
7       30 1
8       33 1
9       40 1
10      80 1
11     100 1

aggregate groups each element in the first argument according to the
specified class.  The third element is a function that is called on
each group.

Neal




More information about the R-help mailing list