Re: [R] Need Help for creating a new variable
Hi Chun, I did do the research and work on for hours ... I try to creat a new variable in my dataset. Yes, looks like you did. Look at ?interaction, which gives you more flexibility than ?:. ## Example diet-sort(rep(x=c(C,T),4)) vesl-rep(x=c(A,P),4) mydata-data.frame(diet,vesl) mydata$trt - interaction(mydata$diet, mydata$vesl) mydata mydata$trt - mydata$diet:mydata$vesl mydata Regards, Mark. Chun-Hao Tu wrote: Hi R users, I did do the research and work on for hours, but I still don't know how to solve my silly problem. I try to creat a new variable in my dataset. such as if diet==C vesl==P then trt=CP; if diet==C vesl==A then trt=CA;. The following is my code (It does not work correctly). Could anyone give me a hint? Appreciate! diet-sort(rep(x=c(C,T),4)) vesl-rep(x=c(A,P),4) mydata-data.frame(diet,vesl) 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 mydata diet vesl trt 1CA CA 2CP CA 3CA CA 4CP CA 5TA CA 6TP CA 7TA CA 8TP CA Thank you very much Chunhao _ [[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. -- View this message in context: http://www.nabble.com/error-bars-tp22092367p22096172.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Need Help for creating a new variable
Dear Chun-Hao, How about this? diet-sort(rep(x=c(C,T),4)) vesl-rep(x=c(A,P),4) mydata-data.frame(diet,vesl) mydata$trt-with(mydata,paste(diet,vesl,sep=)) mydata HTH, Jorge On Thu, Feb 19, 2009 at 2:53 AM, Chun-Hao Tu tc...@hotmail.com wrote: Hi R users, I did do the research and work on for hours, but I still don't know how to solve my silly problem. I try to creat a new variable in my dataset. such as if diet==C vesl==P then trt=CP; if diet==C vesl==A then trt=CA;. The following is my code (It does not work correctly). Could anyone give me a hint? Appreciate! diet-sort(rep(x=c(C,T),4)) vesl-rep(x=c(A,P),4) mydata-data.frame(diet,vesl) 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 mydata diet vesl trt 1CA CA 2CP CA 3CA CA 4CP CA 5TA CA 6TP CA 7TA CA 8TP CA Thank you very much Chunhao _ [[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]] __ 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.
Re: [R] Need Help for creating a new variable
On Thu, Feb 19, 2009 at 9:50 AM, Jorge Ivan Velez jorgeivanve...@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 __ 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.