0

Hi I am relatively new to r and I am trying to figure out how to get age groups from my data. I have a data frame (smsDatraw) age_y is the variable and I would like to create a new variable "age group" with the subsets 18-40, 40-65, 65-85 and >85 i have tried a few ways, but i can't seem to get it to work and if it does run, i can't find the new variable. please help!

This is the code i used and it doesn't work?

smsDatraw$agegroup <- transform(smsDatraw, agegroup = ifelse(smsDatraw$age_y >=18 & age <40 , 1 , ifelse(age_y >=40  & age <65 , 2 , ifelse(age_y >=65 & age<85,  , 3 , ifelse(age_y >= 85 , 4 , 
NA))))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
Pamela
  • 1
  • 2
  • When asking for help, you should provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and desired output. For this problem, you should probably be using the `cut()` function. – MrFlick Feb 23 '17 at 18:42
  • 1
    `transform` and `$agegroup <- ... both can be used to add a column, but don't use both. So `smsDataraw <- transform(...)` will work, or `smsDataraw$agegroup <- ifelse(...)` will work. You don't need both. MrFlick's points are very good guidelines, keep them in mind in the future. Also avoid simply saying that code "doesn't work" - if there is an error or warning, post it! Otherwise try to describe what does happen / how you know the code doesn't work. – Gregor Thomas Feb 23 '17 at 18:46
  • Suggested duplicate: [generate bins from a data frame](http://stackoverflow.com/q/11963508/903061). – Gregor Thomas Feb 23 '17 at 18:46
  • Hi thanks, i have removed the extra variable and this is what I get, sorry I really am new to using this program, appreciate any advice given. to get info on the new variable I assumed I put describe(agegroup) see below. > smsDatraw <- transform(smsDatraw, agegroup = ifelse(smsDatraw$age_y >=18 & age <40 , 1 , ifelse(age_y >=40  & age <65 , 2 , ifelse(age_y >=65 & age<85,  , 3 , ifelse(age_y >= 85 , 4 ,  + NA)))) + describe(agegroup) Error: unexpected symbol in: "NA)))) describe" – Pamela Feb 23 '17 at 18:49
  • `smsDatraw$agegroup <- cut(smsDatraw$age_y , breaks=c(0,18,45,65,85,238), labels = 0:4, right=FALSE, include.lowest = TRUE))` – jogo Feb 23 '17 at 19:06
  • thank you Jogo that is working now! thanks again to you all for the help. – Pamela Feb 23 '17 at 19:15

0 Answers0