[R] Need Help for creating a new variable
Stavros Macrakis
macrakis at alum.mit.edu
Thu Feb 19 16:13:26 CET 2009
On Thu, Feb 19, 2009 at 9:50 AM, Jorge Ivan Velez
<jorgeivanvelez at gmail.com> wrote:
> mydata$trt<-with(mydata,paste(diet,vesl,sep=""))
Besides the above (good!) solution, you might want to understand why
your original solution didn't work:
>> > mydata$trt<-ifelse(mydata$diet=="C" && mydata$vesl=="A", "CA",
>> + ifelse(mydata$diet=="C" && mydata$vesl=="P", "CP",
>> + ifelse(mydata$diet=="T" && mydata$vesl=="A", "TA",
>> + ifelse(mydata$diet=="T" && mydata$vesl=="P", "TP"))))
The problem here is that you are using && rather than &. From the man page:
'&' and '&&' indicate logical AND and '|' and '||' indicate
logical OR. The shorter form performs elementwise comparisons in
much the same way as arithmetic operators. The longer form
evaluates left to right examining only the first element of each
vector.
I trust this makes things clearer.
-s
More information about the R-help
mailing list