[R] Defining contrasts within function

Axel Urbiz axel.urbiz at gmail.com
Tue May 24 02:17:00 CEST 2016


Dear All,

I'd like to change the options("contrasts") within a function, such that
"identity" contrasts
are created for unordered factors. I'm following the idea shown below,
which works fine.

However, when I include these functions in a package (with `contr` being
exported, but
`contr_identity` not exported as it is not intended to be called directly
by the user), I get
the following error message. And this is after loading and attaching the
package with library(). Why is `contr_identity` not found?

Error in get(ctr, mode = "function", envir = parent.frame()) :
  object 'contr_identity' of mode 'function' was not found


contr <- function(x) {
ocontrasts <- options(contrasts = c(unordered = "contr_identity", ordered =
"contr.diff"))
on.exit(options(ocontrasts))
contrasts(x)
}

contr_identity <- function(n, contrasts) {
    contr.treatment(n, contrasts = FALSE)
}

f <- gl(2, 8, labels = c("Control", "Treat"))
contrasts(f)
contr(f)

Thanks,
Axel.

	[[alternative HTML version deleted]]



More information about the R-help mailing list