

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]]


