I have a problem and after spending my weekend on it, I would like to ask for help. To explain the problem, I would like to directly jump into an example:
df <- data.frame(x=rnorm(100), z=rnorm(100), y=rnorm(100), f=rep(1:5,length.out=100 ))
mod <- lm(y ~ x, data=df[df$z>0,])
I want to recycle the data-argument of the model:
dat <- mod$call[['data']]
This gives me:
df[df$z > 0, ]
However, str(dat) will reveal that this is an object of type language. I want to use this expression, however, to access the dataframe that has been used in lm (including the sub-setting), to get the corresponding values of another variable, say f. Note that converting the language object into a character with as.character() will result in a character-vector, and some of the brackets will be lost.
I want to use this inside a function, and what I am looking for is something like this:
foo <- function(fm, "var.name"){
new <- paste(dat, "$", var.name, sep="")
newvar <- eval(parse(text=new), envir=.GlobalEnv)
... do stuff with newvar ...
}
Without sub-setting, this procedure gives me the variable f if I specify var.name as f. With sub-setting, I run into problems with parse due to the fact that dat is now a character-vector with brackets.
As a side-note: the reason why I want to recycle the data-argument from the lm-function instead of just using the same expression with var.name is that I change the sub-setting quite often, and having it recognized from the lm-object makes my life much easier. It also removes a source of error.
I would be highly indebted if anyone could help me out here...
You can just
evalthis expression like this