Skip to content

using names() as a variable in a formula

3 messages · femke, Sundar Dorai-Raj, Duncan Murdoch

#
femke wrote:

            
You're not building a valid formula. Try this:

v <- list()
for(i in 3:5) {
   gr <- names(dataC[i])
   f <- formula(paste(gr, "1", sep = " ~ "))
   v[[gr]] <- variogram(f, loc = ~ x + y, dataC)
}

BTW, since variogram is not in the base package it would also be helpful 
in the future if you add that you are using the spatial package.

HTH,
Sundar
#
On Wed, 18 Feb 2004 15:44:59 -0500, "femke" <femke at geog.umd.edu> wrote
What package is variogram in?  The one in spatial takes different
args.
Doing this sort of thing is tricky.  Your names(dataC[3]) is a
character string; you want something there that would be interpreted
as a name instead.  However, as.name(names(dataC[3])) isn't enough.

I think you need to build up the formula using fairly low level stuff,
something like this:

   formula <- as.call(list(as.name('~'), as.name(names(dataC[3])),
quote(1)))

and then do

  variogram(formula, ....)

but a formula constructed this way doesn't work in lm(), so it may not
work in variogram either.

Duncan Murdoch