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? Thanks in advance, Sigalit. [[alternative HTML version deleted]]
Hi, try this: #df <- data.frame(age=sample(20:100, 20, rep=T), age1=gl(4, length(age)/4)) df$age1[which(age %in% 12:31)] <- 1 df$age1[which(age %in% 32:51)] <- 2 df$age1[which(age %in% 52:71)] <- 3 df$age1[which(age %in% 72:99)] <- 4 -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O On 02/11/2007, sigalit mangut-leiba <smangut@gmail.com> 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? > > Thanks in advance, > > Sigalit. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Fri, 2007-11-02 at 22:02 +0200, 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? > > Thanks in advance, > > Sigalit.The if() function can only return a single result, so you would have to create something along the lines of an if...else construct. Alternatively, you can use cut(): age <- 12:99> cut(age, breaks = c(12, 32, 52, 72, 100), labels = 1:4, include.lowest = TRUE)[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 [35] 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 [69] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 Levels: 1 2 3 4 Note that this returns a factor. If you want an actual integer value, you would have to coerce the result. See ?cut for more information. HTH, Marc Schwartz
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 ------------------------------