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.
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"
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.