using a string in doBy command
I am using the summaryBy() (doBy package) function inside a function I am writing. I want to define the variable (as a string) with respect to which the summary needs to be calculated. When I define a string and pass it to summaryBy function it doesn't work. Here is an example from the help file of summaryBy function .
data(dietox)
dietox12<- subset(dietox,Time==12)
Following co开发者_JS百科de works
summaryBy(Weight+Feed~Evit+Cu,data=dietox12,FUN=mean)
However the following doesn't work
vardef<-'Evit';
summaryBy(Weight+Feed~vardef+Cu,data=dietox12,FUN=mean)
I know why it doesn't work. I wanted to know whether there is another way. Of course I can use the ddply function in stead of the summaryBy function.
Thank you.If you want to build formulas dynamically, the easiest way to go is to build it completely as a string.
form<-formula(paste("Weight+Feed~", vardef, "+Cu", sep=""))
summaryBy(form,data=dietox12,FUN=mean)
There are other options, but this is the easiest.
精彩评论