[R] Need Help for creating a new variable
Mark Difford
mark_difford at yahoo.co.uk
Thu Feb 19 09:46:47 CET 2009
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
> 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 at 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.
More information about the R-help
mailing list