Hi everyone ! Could you please help me with this problem ? I??ve trying to write a code that assign to a variable the content from another, but all I??ve got is a message error. For example: if (age <=10) {group == 1} else if (age > 10 & age <= 20) {group == 2} else {group == 3} Syntax error Or if (age <=10) {group == 1} else (age > 10 & age <= 20) {group == 2} else {group == 3} Syntax error I know that is possible to find the solution by ifelse command or even recode command, but I??d like to use this way, because I can add another variable as a new condition and I believe to expand the possibilites. Thanks, Mauricio
Carlos Maur??cio Cardeal Mendes wrote:> Hi everyone ! > > Could you please help me with this problem ? > > I??ve trying to write a code that assign to a variable the content from > another, but all I??ve got is a message error. For example: > > if (age <=10) {group == 1} > else if (age > 10 & age <= 20) {group == 2} > else {group == 3} > > Syntax error > > Or > > if (age <=10) {group == 1} > else (age > 10 & age <= 20) {group == 2} > else {group == 3} > > Syntax error > > I know that is possible to find the solution by ifelse command or even > recode command, but I??d like to use this way, because I can add another > variable as a new condition and I believe to expand the possibilites. > > Thanks, > Mauricio >Because the following line is syntatically correct: if (age <=10) {group == 1} the R parser does not expect the following: else (age > 10 & age <= 20) {group == 2} else {group == 3} causing a sytax error. Instead, you want: if (age <=10) { group == 1 } else (age > 10 & age <= 20) { group == 2 } else { group == 3 } HTH, --sundar
[This email is either empty or too large to be displayed at this time]
Hallo On 13 Sep 2005 at 10:29, Carlos Maur??cio Cardeal Mende wrote:> Hi everyone ! > > Could you please help me with this problem ? > > I??ve trying to write a code that assign to a variable the contentfrom> another, but all I??ve got is a message error. For example: > > if (age <=10) {group == 1} > else if (age > 10 & age <= 20) {group == 2} > else {group == 3}if you put your statement on one line it works (at least it does not give you syntax error) but the result is hardly what you really expect age<-sample(seq(10,50,10), 20, replace=T) if (age <=10) {group <- 1} else if (age > 10 & age <= 20) {group <- 2} else {group <- 3} if (age <=10) {group == 1} else if (age > 10 & age <= 20) {group == 2} else {group == 3} Maybe you want something like group<-as.numeric(cut(age,c(0,10,20,100))) but it is only guess HTH Petr> > Syntax error > > Or > > if (age <=10) {group == 1} > else (age > 10 & age <= 20) {group == 2} > else {group == 3} > > Syntax error > > I know that is possible to find the solution by ifelse command oreven> recode command, but I??d like to use this way, because I can add > another variable as a new condition and I believe to expand the > possibilites. > > Thanks, > Mauricio > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz
First, "==" is logical comparison, so if you want to create a variable based on both "age" and "group" you can do that. However, it looks like you want to define the variable "group", so you want to use "<-" or "=" for that. Second, if you're typing this at a command prompt, you need to make sure you tell R you're not finished when it looks like you could be. There are several ways to do this. One is to put everything inside braces; another is to deliberately leave lines incomplete, like if (age <= 10) { group <- 1 } else { if (age <= 20) { group <- 2 } else group <- 3 } Third, this will work for a vector of length 1. If you want to take a vector "age" and produce a corresponding vector "group", you'll need to put this in a loop, or use "lapply", or some iteration. Fourth, you can also write the above as> group <- if (age <= 10) 1 else if (age <= 20) 2 else 3that is, if() returns a value you can assign. Finally, besides "ifelse" you can use "cut" for this particular task. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Carlos Maur??cio Cardeal Mendes Sent: Tuesday, September 13, 2005 9:29 AM To: r-help at stat.math.ethz.ch Subject: [R] if() command Hi everyone ! Could you please help me with this problem ? I??ve trying to write a code that assign to a variable the content from another, but all I??ve got is a message error. For example: if (age <=10) {group == 1} else if (age > 10 & age <= 20) {group == 2} else {group == 3} Syntax error Or if (age <=10) {group == 1} else (age > 10 & age <= 20) {group == 2} else {group == 3} Syntax error I know that is possible to find the solution by ifelse command or even recode command, but I??d like to use this way, because I can add another variable as a new condition and I believe to expand the possibilites. Thanks, Mauricio ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Ok Petr, I run your suggestion and I got this message: > age<-sample(seq(10,50,10), 20, replace=T) > > if (age <=10) {group <- 1} else if (age > 10 & age <= 20) {group <- 2} else {group <- 3} Warning message: the condition has length > 1 and only the first element will be used in: if (age <= 10) { What does it means ? And when I look to the database I have no new classification ! Could you help please ? Mauricio Petr Pikal escreveu:> Hallo > > > On 13 Sep 2005 at 10:29, Carlos Maur??cio Cardeal Mende wrote: > >> Hi everyone ! >> >> Could you please help me with this problem ? >> >> I??ve trying to write a code that assign to a variable the content from >> another, but all I??ve got is a message error. For example: >> >> if (age <=10) {group == 1} >> else if (age > 10 & age <= 20) {group == 2} >> else {group == 3} > > if you put your statement on one line it works (at least it does not > give you syntax error) but the result is hardly what you really expect > > age<-sample(seq(10,50,10), 20, replace=T) > > if (age <=10) {group <- 1} else if (age > 10 & age <= 20) {group <- 2} > else {group <- 3} > if (age <=10) {group == 1} else if (age > 10 & age <= 20) {group == 2} > else {group == 3} > > Maybe you want something like > > group<-as.numeric(cut(age,c(0,10,20,100))) > > but it is only guess > > HTH > Petr > >> >> Syntax error >> >> Or >> >> if (age <=10) {group == 1} >> else (age > 10 & age <= 20) {group == 2} >> else {group == 3} >> >> Syntax error >> >> I know that is possible to find the solution by ifelse command or even >> recode command, but I??d like to use this way, because I can add >> another variable as a new condition and I believe to expand the >> possibilites. >> >> Thanks, >> Mauricio >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide! >> http://www.R-project.org/posting-guide.html > > > Petr Pikal > petr.pikal at precheza.cz > >------------------------------------------------------------------------ > >No virus found in this incoming message. >Checked by AVG Anti-Virus. >Version: 7.0.344 / Virus Database: 267.10.21/96 - Release Date: 10/9/2005 > >
"if" is not vectorised and "age" is a vector. Try the following test: if(c(TRUE, FALSE)) "TRUE" else "FALSE" You really need to use "ifelse". ifelse(c(TRUE, FALSE), "TRUE", "FALSE") As others have suggested, you might want to look at ?cut. --sundar Carlos Mauricio Cardeal Mendes wrote:> Ok Petr, I run your suggestion and I got this message: > > > age<-sample(seq(10,50,10), 20, replace=T) > > > > if (age <=10) {group <- 1} else if (age > 10 & age <= 20) {group <- > 2} else {group <- 3} > Warning message: > the condition has length > 1 and only the first element will be used in: > if (age <= 10) { > > What does it means ? > > And when I look to the database I have no new classification ! > > Could you help please ? > > Mauricio > > Petr Pikal escreveu: > > >>Hallo >> >> >>On 13 Sep 2005 at 10:29, Carlos Maur??cio Cardeal Mende wrote: >> >> >>>Hi everyone ! >>> >>>Could you please help me with this problem ? >>> >>>I??ve trying to write a code that assign to a variable the content from >>>another, but all I??ve got is a message error. For example: >>> >>>if (age <=10) {group == 1} >>>else if (age > 10 & age <= 20) {group == 2} >>>else {group == 3} >> >>if you put your statement on one line it works (at least it does not >>give you syntax error) but the result is hardly what you really expect >> >>age<-sample(seq(10,50,10), 20, replace=T) >> >>if (age <=10) {group <- 1} else if (age > 10 & age <= 20) {group <- 2} >>else {group <- 3} >>if (age <=10) {group == 1} else if (age > 10 & age <= 20) {group == 2} >>else {group == 3} >> >>Maybe you want something like >> >>group<-as.numeric(cut(age,c(0,10,20,100))) >> >>but it is only guess >> >>HTH >>Petr >> >> >>>Syntax error >>> >>>Or >>> >>>if (age <=10) {group == 1} >>>else (age > 10 & age <= 20) {group == 2} >>>else {group == 3} >>> >>>Syntax error >>> >>>I know that is possible to find the solution by ifelse command or even >>>recode command, but I??d like to use this way, because I can add >>>another variable as a new condition and I believe to expand the >>>possibilites. >>> >>>Thanks, >>>Mauricio >>> >>>______________________________________________ >>>R-help at stat.math.ethz.ch mailing list >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>PLEASE do read the posting guide! >>>http://www.R-project.org/posting-guide.html >> >> >>Petr Pikal >>petr.pikal at precheza.cz >> >>------------------------------------------------------------------------ >> >>No virus found in this incoming message. >>Checked by AVG Anti-Virus. >>Version: 7.0.344 / Virus Database: 267.10.21/96 - Release Date: 10/9/2005 >> >> > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Wed, 14 Sep 2005, Carlos Mauricio Cardeal Mendes wrote:> Ok Petr, I run your suggestion and I got this message: > > > age<-sample(seq(10,50,10), 20, replace=T) > > > > if (age <=10) {group <- 1} else if (age > 10 & age <= 20) {group <- > 2} else {group <- 3} > Warning message: > the condition has length > 1 and only the first element will be used in: > if (age <= 10) { > > What does it means ? > > And when I look to the database I have no new classification ! >Although the syntax issue is real, if() is not the way to go if you are comparing a vector with a scalar; if() will only compare the first element of the vector with the scalar. The ifelse() function is vectorised:> age<-sample(seq(10,50,10), 20, replace=T) > group_ifelse <- ifelse(age > 10, ifelse(age > 20, 3, 2), 1) > group_ifelse[1] 3 2 3 3 3 2 3 3 1 2 3 1 1 1 3 2 3 2 3 3 or maybe even better, use the cut function to create a grouping factor:> group_cut <- cut(age, breaks=c(0,10,20,100), include.lowest=TRUE) > group_cut[1] (20,100] (10,20] (20,100] (20,100] (20,100] (10,20] (20,100] (20,100] [9] [0,10] (10,20] (20,100] [0,10] [0,10] [0,10] (20,100] (10,20] [17] (20,100] (10,20] (20,100] (20,100] Levels: [0,10] (10,20] (20,100]> age[1] 30 20 30 40 30 20 50 50 10 20 30 10 10 10 30 20 40 20 50 40> as.integer(group_cut)[1] 3 2 3 3 3 2 3 3 1 2 3 1 1 1 3 2 3 2 3 3 Sometimes you need to enclose cut() within ordered(), and if there are empty intervals, you may not get what you expect from the integer representation of the result. Yet another elegant function is findInterval():> group_findInterval <- findInterval(age, c(0,10.001,20.001,100)) > group_findInterval[1] 3 2 3 3 3 2 3 3 1 2 3 1 1 1 3 2 3 2 3 3 Hope this helps> Could you help please ? > > Mauricio > > Petr Pikal escreveu: > > > Hallo > > > > > > On 13 Sep 2005 at 10:29, Carlos Maur??cio Cardeal Mende wrote: > > > >> Hi everyone ! > >> > >> Could you please help me with this problem ? > >> > >> I??ve trying to write a code that assign to a variable the content from > >> another, but all I??ve got is a message error. For example: > >> > >> if (age <=10) {group == 1} > >> else if (age > 10 & age <= 20) {group == 2} > >> else {group == 3} > > > > if you put your statement on one line it works (at least it does not > > give you syntax error) but the result is hardly what you really expect > > > > age<-sample(seq(10,50,10), 20, replace=T) > > > > if (age <=10) {group <- 1} else if (age > 10 & age <= 20) {group <- 2} > > else {group <- 3} > > if (age <=10) {group == 1} else if (age > 10 & age <= 20) {group == 2} > > else {group == 3} > > > > Maybe you want something like > > > > group<-as.numeric(cut(age,c(0,10,20,100))) > > > > but it is only guess > > > > HTH > > Petr > > > >> > >> Syntax error > >> > >> Or > >> > >> if (age <=10) {group == 1} > >> else (age > 10 & age <= 20) {group == 2} > >> else {group == 3} > >> > >> Syntax error > >> > >> I know that is possible to find the solution by ifelse command or even > >> recode command, but I??d like to use this way, because I can add > >> another variable as a new condition and I believe to expand the > >> possibilites. > >> > >> Thanks, > >> Mauricio > >> > >> ______________________________________________ > >> R-help at stat.math.ethz.ch mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide! > >> http://www.R-project.org/posting-guide.html > > > > > > Petr Pikal > > petr.pikal at precheza.cz > > > >------------------------------------------------------------------------ > > > >No virus found in this incoming message. > >Checked by AVG Anti-Virus. > >Version: 7.0.344 / Virus Database: 267.10.21/96 - Release Date: 10/9/2005 > > > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
Looping would look like: group <- vector(length=length(age)) for (i in 1:length(age)) { group[i] <- if (age[i] <= 10) 1 else if (age[i] <= 20) 2 else 3 } Another way to do this is to write a function, say "category", like category <- function(x) if(x <= 10) 1 else if (x <= 20) 2 else 3 and then apply the function to all elements of "age" like group <- sapply(age,category) (This is a common way to vectorize a function.) Reid Huntsinger -----Original Message----- From: Carlos Mauricio Cardeal Mendes [mailto:mcardeal at ufba.br] Sent: Wednesday, September 14, 2005 1:21 PM To: Huntsinger, Reid Cc: r-help at stat.math.ethz.ch Subject: Re: [R] if() command Hello reid ! About your third explanation, could you please write the complete code including that option: a loop ? Forgiveme, I'm trying to learn R and my mind is full of other statistical program syntax. And I'd like very very much to improve my knowledge using R and maybe contribute to someone, someday, somehow. Thanks, again Mauricio Huntsinger, Reid escreveu:>First, "==" is logical comparison, so if you want to create a variablebased>on both "age" and "group" you can do that. However, it looks like you want >to define the variable "group", so you want to use "<-" or "=" for that. > >Second, if you're typing this at a command prompt, you need to make sureyou>tell R you're not finished when it looks like you could be. There are >several ways to do this. One is to put everything inside braces; another is >to deliberately leave lines incomplete, like > >if (age <= 10) { > group <- 1 >} else { > if (age <= 20) { > group <- 2 > } else group <- 3 >} > >Third, this will work for a vector of length 1. If you want to take avector>"age" and produce a corresponding vector "group", you'll need to put thisin>a loop, or use "lapply", or some iteration. > >Fourth, you can also write the above as > > > >>group <- if (age <= 10) 1 else if (age <= 20) 2 else 3 >> >> > >that is, if() returns a value you can assign. > >Finally, besides "ifelse" you can use "cut" for this particular task. > >Reid Huntsinger > > >-----Original Message----- >From: r-help-bounces at stat.math.ethz.ch >[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Carlos Maur??cio >Cardeal Mendes >Sent: Tuesday, September 13, 2005 9:29 AM >To: r-help at stat.math.ethz.ch >Subject: [R] if() command > > >Hi everyone ! > >Could you please help me with this problem ? > >I??ve trying to write a code that assign to a variable the content from >another, but all I??ve got is a message error. For example: > >if (age <=10) {group == 1} >else if (age > 10 & age <= 20) {group == 2} >else {group == 3} > >Syntax error > >Or > >if (age <=10) {group == 1} >else (age > 10 & age <= 20) {group == 2} >else {group == 3} > >Syntax error > >I know that is possible to find the solution by ifelse command or even >recode command, but I??d like to use this way, because I can add another >variable as a new condition and I believe to expand the possibilites. > >Thanks, >Mauricio > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! >http://www.R-project.org/posting-guide.html > > > > > >------------------------------------------------------------------------------>Notice: This e-mail message, together with any attachments...{{dropped}}
[This email is either empty or too large to be displayed at this time]
Hi Sorry, I am not sure why sometimes is a text from my answeres stripped off. On 14 Sep 2005 at 14:09, Carlos Mauricio Cardeal Mende wrote:> Ok Petr, I run your suggestion and I got this message: > > > age<-sample(seq(10,50,10), 20, replace=T) > > > > if (age <=10) {group <- 1} else if (age > 10 & age <= 20){group <-> > 2} else {group <- 3} > Warning message: > the condition has length > 1 and only the first element will beused> in: if (age <= 10) { > > What does it means ?Others has already answered it but I told you that if command is not vhat you probably want and suggested to use cut e.g. cutvector <- c(0,10,20,100) group <- as.numeric(cut(age,cutvector)) You can change your cutvector according to your wish anytime before calling the second function HTH Petr> > And when I look to the database I have no new classification ! > > Could you help please ? > > Mauricio > > Petr Pikal escreveu: > > > Hallo > > > > > > On 13 Sep 2005 at 10:29, Carlos Maur??cio Cardeal Mendewrote:> > > >> Hi everyone ! > >> > >> Could you please help me with this problem ? > >> > >> I??ve trying to write a code that assign to a variable thecontent> >> from another, but all I??ve got is a message error. Forexample:> >> > >> if (age <=10) {group == 1} > >> else if (age > 10 & age <= 20) {group == 2} > >> else {group == 3} > > > > if you put your statement on one line it works (at least it doesnot> > give you syntax error) but the result is hardly what you really > > expect > > > > age<-sample(seq(10,50,10), 20, replace=T) > > > > if (age <=10) {group <- 1} else if (age > 10 & age <= 20){group <-> > 2} else {group <- 3} if (age <=10) {group == 1} else if (age >10 &> > age <= 20) {group == 2} else {group == 3} > > > > Maybe you want something like > > > > group<-as.numeric(cut(age,c(0,10,20,100))) > > > > but it is only guess > > > > HTH > > Petr > > > >> > >> Syntax error > >> > >> Or > >> > >> if (age <=10) {group == 1} > >> else (age > 10 & age <= 20) {group == 2} > >> else {group == 3} > >> > >> Syntax error > >> > >> I know that is possible to find the solution by ifelse commandor> >> even recode command, but I??d like to use this way, because Ican> >> add another variable as a new condition and I believe toexpand the> >> possibilites. > >> > >> Thanks, > >> Mauricio > >> > >> ______________________________________________ > >> R-help at stat.math.ethz.ch mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide! > >> http://www.R-project.org/posting-guide.html > >Petr Pikal petr.pikal at precheza.cz