0

I think I'm already half way to what I want to be doing by using (just need a little help doing the second part):

clients[which(clients$Age >= 18 & clients$Age <= 24),]

This groups everyone from 18 years of age to 24 years of age (inclusive) and lists all the rows which match in my data frame.

I just want to go one step further and assign every row which matches this constraint with a level of a factor variable; say 'I'. And then everyone from 25 - 34 can be part of 'II', 35 - 44 part of 'III', etc, etc.

The ultimate goal is to just make it easy for me to plot the frequency from different age groups next to one another - I feel like making them each unique levels in a factor variable would be a good start.

Any ideas?

Troy
  • 683
  • 1
  • 7
  • 8

1 Answers1

3

You could use cut to specify multiple levels at once:

cut( clients$Age,
     breaks = c( 18, 25, 35, 45 ),
     include.lowest = TRUE,
     labels = c( "I", "II", "III" ) )

An example, using every integer between 18 and 45.

cut( seq.int( 18, 45, 1 ),
     breaks = c( 18, 25, 35, 45 ),
     include.lowest = TRUE,
     labels = c( "I", "II", "III" ) )
 [1] I   I   I   I   I   I   I   I   II  II  II  II  II  II  II  II  II  II  III III III III
[23] III III III III III III
Levels: I II III
rosscova
  • 5,430
  • 1
  • 22
  • 35