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 > 1 C A CA > 2 C P CA > 3 C A CA > 4 C P CA > 5 T A CA > 6 T P CA > 7 T A CA > 8 T P 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.