# Help with creating indicator age variable

#### Foargo9x

##### New Member
0
down vote
favorite
I am working on this project which needs to be done by tomorrow and I am stuck on creating the indicator variable.

I am supposed to create indicator age variables for respondents that fit into the following age buckets. -

Age 18-24 -
Age 25-44 -
Age 45-64 -
Age 65 +

I figured that I need to use the cut function, however, I do not know how to cut the variable manually. All I got was:

> age1.f <- cut(age1, breaks = 4)
> age1.f

So basically, it create 4 categories for age automatically for me.
Any suggestion? Thank you so much

#### trinker

##### ggplot2orBust
If you use:

Code:
?cut
You'll see you can use:

Code:
breaks
either a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which x is to be cut.
So supply the vector of breaks here.

#### Foargo9x

##### New Member
If you use:

Code:
?cut
You'll see you can use:

So supply the vector of breaks here.

Hi, I have trouble naming the levels of my age variable. This tis the code I have and it is not working. Can you please take a look?

> cage.drop2 <- cut(age.drop, br=c(18,24,44,64,102), labels = c("A", "B", "C", "D""))

When I have it as
> cage.drop <- cut(age.drop, br=c(17,24,44,64,102))
it works but when I added the labels it didnt

#### trinker

##### ggplot2orBust
I don't have anything to work with. No data or fake data. That will get this question moving along.

Also...

When you're posting code, dataframes or computer output it's helpful to wrap this information in code tags by:
1. either clicking the pound (#) sign icon or
2. wrap with [NOPARSE]
Code:
some code
[/NOPARSE]

which produces:
Code:
some code

#### Foargo9x

##### New Member
I don't have anything to work with. No data or fake data. That will get this question moving along.

Also...

When you're posting code, dataframes or computer output it's helpful to wrap this information in code tags by:
1. either clicking the pound (#) sign icon or
2. wrap with [NOPARSE]
Code:
some code
[/NOPARSE]

which produces:
Code:
some code
Hi thank you for the quick response,
Fox example, lets say we have a variable age: 18,35,64,22,34,21,77,50,43,28,39.
How can I create 4 categories for this variable and name them as: A: 18-24, B: 25-44, C: 45-64, D: 64+.

I was able to create 4 categories but was not abel to give them names.

#### trinker

##### ggplot2orBust
I think what you meant to say was...

Hi thank you for the quick response,
Fox example, lets say we have a variable age:
Code:
x <- c(18,35,64,22,34,21,77,50,43,28,39)
How can I create 4 categories for this variable and name them as: A: 18-24, B: 25-44, C: 45-64, D: 64+.

#### trinker

##### ggplot2orBust
Code:
x <- c(18, 35,64,22,34,21,77,50,43,28,39)
cut(x, c(0, 24, 44, 64, Inf), c("A: 18-24", "B: 25-44", "C: 45-64", "D: 64+"))

#### Foargo9x

##### New Member
Code:
x <- c(18, 35,64,22,34,21,77,50,43,28,39)
cut(x, c(0, 24, 44, 64, Inf), c("A: 18-24", "B: 25-44", "C: 45-64", "D: 64+"))
Thank you thank you thank you. It works.

I was wondering if you know about the top 3 box in R? I have no idea what it is and the next question is:

c) Top 3 box refers to the highest three values on a response scale. A top 3 box variable takes the value 1 if a respondent selected one of the top 3 responses. Create top 3 box versions of the following variables (from the attached legend): size, attributes...

I am trying to google "top 3 box" but nothing relevant comes up. The closest thing on my mind right now is a boxplot.

#### Foargo9x

##### New Member
Code:
x <- c(18, 35,64,22,34,21,77,50,43,28,39)
cut(x, c(0, 24, 44, 64, Inf), c("A: 18-24", "B: 25-44", "C: 45-64", "D: 64+"))
Hi can you please disregard my previous message? I was able to understand it now.
So I am trying to create 24 dummy variables for a dataset. The question is:

Top 3 box refers to the highest three values on a response scale. A top 3 box variable takes the value 1 if a respondent selected one of the top 3 responses. Create top 3 box versions of the following variables (from the attached legend): - Brand Imagery Attributes (imageattr1 … 24)

Brand Familiarity (familiarity)
Brand Favorability (favorability)
Brand Consideration (consideration) Bonus: Write a function in R that can recode multiple variables to top 3 box at the same time.

What I am doing at the moment is that I have to manually create dummy variable for each of these variables:

dummy1 <- as.numeric(familiarity >= 8)

dummy2 <- as.numeric(favorability >= 8)....

dummy24 <- as.numeric(..... >=8)

Is there away I can do to make R create these dummies for me in one go? All these 24 variables have the same response scale (0-10) and there for I am trying to coding the responses of 8,9 and 10 as 1 and the rest as 0. Thank you.