jeudi 23 février 2017

Creating new variable

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))))

Aucun commentaire:

Enregistrer un commentaire