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))
[instead of$and adjust yourddplycalls to not use a formula or to build the correct formula withpasteandexpression. The long answer is to read part of Hadley's Advanced R book, paying special attention to the "Non-Standard Evaluation in Subset" section.