I have a more complicated function that I am working on. I want to pass a variable around inside of my function to do several things. The part that is not working is trying to access an object that has my variable's name embedded in it.
A simple example would be as follows:
## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
D9.fit.lm <- lm(weight ~ group)
my.coeff <- function(mname)
{
b <- round(mname.fit.lm$coef[1],2)
m <- round(mname.fit.lm$coef[2],4)
r2 <- round(summary(mname.fit.lm)$r.squared,2)
cbind(b, m, r2)
}
So if I try this bit of code like this:
my.coeff("D9")
Then I get the following error:
Error in my.coeff("D9") : object 'mname.fit.lm' not found
I really my function to work and it should look like this output
cbind(round(D9.fit.lm$coef[1],2),round(D9.fit.lm$coef[2],4),round(summary(D9.fit.lm)$r.squared,2))
[,1] [,2] [,3]
(Intercept) 5.03 -0.371 0.07
Thanks !