[R] formula and model.frame
Charles C. Berry
cberry at tajo.ucsd.edu
Wed Oct 21 19:47:31 CEST 2009
Emulate lm.
myFun <-
function(formula, data, na.action, id, ...){
mf <- match.call(expand.dots = FALSE)
m <- match(c("formula", "data", "id", "na.action"), names(mf), 0L)
mf <- mf[c(1L, m)]
mf$drop.unused.levels <- TRUE
mf[[1L]] <- as.name("model.frame")
mf <- eval(mf, parent.frame())
mf
}
myFun(score ~ grade, qqq, id=idVar)
HTH,
Chuck
On Wed, 21 Oct 2009, Doran, Harold wrote:
> Suppose I have the following function
>
> myFun <- function(formula, data){
> f <- formula(formula)
> dat <- model.frame(f, data)
> dat
> }
>
> Applying it with this sample data yields a new dataframe:
>
> qqq <- data.frame(grade = c(3, NA, 3,4,5,5,4,3), score = rnorm(8), idVar = c(1:8))
>
> dat <- myFun(score ~ grade, qqq)
>
> However, what I would like is for the resulting dataframe (dat) to include as a column idVar. Naively, I could do
>
> dat <- myFun(score ~ grade + idVar, qqq)
>
> This gives what I'm after in terms of the resulting data, but doesn't make sense within the context of the model I am working on since idVar is not one of the conditioning variables used, it has a different purpose altogether. So, I was thinking another way is to attach it somehow afterwards. Something like:
>
> myFun <- function(formula, data, id, na.action){
> f <- formula(formula)
> idVar <- data[, id]
> dat <- model.frame(f, data, na.action = na.action)
> dat[, id] <- idVar
> dat
> }
>
> myFun(score ~ grade, qqq, id = 'idVar', na.action = NULL)
>
> Of course, I intentionally use na.action = NULL here because the following occurs, of course
>
>> myFun(score ~ grade, qqq, id = 'idVar', na.action = na.omit)
> Error in `[<-.data.frame`(`*tmp*`, , id, value = 1:8) :
> replacement has 8 rows, data has 7
>
> I see a few workarounds, but I am certain there is a cleaner way to accomplish this.
>
> Harold
>
>
>> sessionInfo()
> R version 2.9.0 (2009-04-17)
> i386-pc-mingw32
>
> locale:
> LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
>
> attached base packages:
> [1] splines stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] lme4_0.999375-28 Matrix_0.999375-25 lattice_0.17-22 xtable_1.5-5 adapt_1.0-4 MiscPsycho_1.4
> [7] statmod_1.3.8
>
> loaded via a namespace (and not attached):
> [1] grid_2.9.0 tools_2.9.0
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list