Reputation: 657
I have created some simple functions in R that take data and return variables. I'd appreciate instruction on taking this to another level using the code below as illustration: There's a data frame 'df' which contains variables DOFW, CDFME (there are others, but two will suffice for this example. I'd like to pass to the function the name of the variable and have it build the barplot. Basically I want to create a function that performs the actions from z<- through abline and call that function for each variable. Thank you.
df<-data.frame(y.train,x.train)
z<-ddply(df,~DOFW,summarise,median=median(PCTCHG))
row.names(z)<-z$DOFW
z$DOFW<-NULL
barplot(t(as.matrix(z)),main="Median Return by DOFW",xlab="DOFW")
abline(h=median(y.train))
z<-ddply(df,~CDFME,summarise,median=median(PCTCHG))
row.names(z)<-z$CDFME
z$CDFME<-NULL
barplot(t(as.matrix(z)),main="Median Return by CDFME",xlab="CDFME")
abline(h=median(y.train))
Upvotes: 0
Views: 466
Reputation: 206207
Actually you don't really need non-standard evaluation since ddply
allows you to pass a string for the variable name rather than a formula. Here's how you could do that.
#sample data
dd<-data.frame(a=rep(1:5, 2), b=2:11, c=runif(10))
#define function
library(plyr)
myplot<-function(coln) {
z<-ddply(dd, coln, summarise, median=median(c))
barplot(z[,2], main=paste("Median Return By", coln))
abline(h=median(dd$c))
}
#make plots
myplot("a")
myplot("b")
and the easiest way to get the names of the columns as a character vector is names(dd)
.
Upvotes: 1