I would like to transform in R this sas code to create a new variable from an existing one:
if eta=0 then eta1=0;
if 0<eta<=4 then eta1=1;
if 4<eta<=9 then eta1=2;
if 9<eta<=14 then eta1=3;
if 14<eta<=19 then eta1=4;
if 19<eta<=24 then eta1=5;
if 24<eta<=29 then eta1=6;
.... and so on..
I tried with ifelse but here my new variable eta1 is not binary..
then i tried this:
eta1[eta<1]<- 0
eta1[eta>=1 & eta<=4]<- 1
eta1[eta>=5 & eta<=9]<- 2
and this:
pop%>%
mutate(
eta1=case_when(
eta%in% c(1,2,3,4)~1,
eta%in% c(5,6,7,8,9)~2
)
)
and this
pop%>%
mutate(
eta1=case_when(
eta%in% c("1","2","3","4")~"1",
eta%in% c("5","6","7","8","9")~"2"
)
)
but these don't work.. so I can't understand how to create it without losing infos
How can I fix it?
Thank you!