[R] if() command

Petr Pikal petr.pikal at precheza.cz
Thu Sep 15 13:27:43 CEST 2005


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 be  
used 
> 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 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




More information about the R-help mailing list