[R] Grouping data

Marc Schwartz marc_schwartz at comcast.net
Wed Jan 16 21:24:45 CET 2008


K. Elo wrote:
> Hi,
>
> I am quite new to R (but like it very much!), so please apologize if
> this is a too simple question.
>
> I have a large data frame consisting of data from a survey. There is,
> for example, information about age and education (a numeric value from
> 1-9). Now I would like to extract the total amount of each type of
> education within different age groups (e.g. from 18 to 25, from 25 to
> 35 etc.). How could I achieve this? (I have been thinking about
> using 'subset', but if there are better ideas they are welcome :) )
>
> An example might clarify my point. Let's assume the following data:
> #	age	edu
> 1	25	2
> 2	33	5
> 3	22	3
> 4	19	1
> 5	21	3
> 6	30	4
> 7	32	4
> 8	31	1
>
> What I want to have is:
>
> edu	18-25	25-35 ...
> 1	1	1
> 2	1	0
> 3	2	0
> 4	0	2
> 5	0	1
>
> Thanks in advance&  kind regards,
> Kimmo

See ?cut which will enable you to take a continuous vector and convert 
it into a factor based upon breakpoints. Use this combined with ?table 
which will give you a cross tabulation. Something along the lines of the 
following, presuming that your data is in a data frame called 'DF':

 > with(DF, table(edu, cut(age, breaks = c(18, 25, 35))))

edu (18,25] (25,35]
   1       1       1
   2       1       0
   3       2       0
   4       0       2
   5       0       1

Note the default symbology of the returned labels indicating whether or 
not the interval breakpoints are open or closed in each grouping. This 
is covered in the help for cut(). Pay attention to the 'include.lowest' 
and 'right' arguments.

Note also the 'trick' of using with() here, so that the column names are 
evaluated within the *environment* of the dataframe. See ?with for more 
information there.

HTH,

Marc Schwartz




More information about the R-help mailing list