[R] writing a categorical var. with condition

(Ted Harding) Ted.Harding at manchester.ac.uk
Fri Nov 2 22:33:52 CET 2007


On 02-Nov-07 21:02:57, sigalit mangut-leiba wrote:
> Hello,
> I want to create a new variable which includes 4 age categories
> in this way: 
> if (age>=12 && age<32) age1==1
> if (age>=32 && age<52) age1==2
> if (age>=52 && age<72) age1==3
> if (age>=72 && age<100) age1==4
> 
> but I get the results only for the first observation.
> 
> how can I apply this condition to all observations?

If A is a vector of ages, one way to do it would be

age1 <- 1*(A>=12)&(A<32) + 2*(A>=32)&(A<52) +
        3*(A>=52)&(A<72) + 4*(A>=72)&(A<100)

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 02-Nov-07                                       Time: 22:33:48
------------------------------ XFMail ------------------------------



More information about the R-help mailing list