creating and adding a new variable into a data frame by using an if statement

If one had a group of data and if they wanted to take a variable in that data frame and group them into categories and then add those categories as variables on the existing data frame by using an if function, how would one go about doing so?

Cause I'm thinking i write the if function, specify the requirements and then enter the function to add that to the data frame in the curly brackets, but the I don't know how I would apply that to all the data in the variable.

Please provide a minimum working example, otherwise not.

Please remove the verbatim assignment question from your post as it violates the community's homework policy:

What have you tried so far? Are you stuck on a particular task? We can give advice if you ask a specific question.

this homework stipulation is likely misleading as it implies a suboptimal tool should be used.
I will rewrite the question for you so you will have a higher change of success if you study the subject.
"Write an ifelse()-statement (and supply the code) that will create a new variable called agecat (added to the data frame Hddata), which will be calculated from the existing age variable, by grouping age into the following age groups: i. [25:40) years ii. [40:65) years iii. [65:80) years"

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

okay will do. Thank you so much.

Thank you for generalizing your question. @nirgrahamuk has already given you a hint. The if function is not vectorized (i.e. it only works on a single value at a time) but ifelse() is. If your assignment allows it, you could try using this function instead.