[R] options("contrasts")

Duncan Murdoch murdoch at stats.uwo.ca
Tue Aug 26 13:13:15 CEST 2008

On 26/08/2008 6:30 AM, David Epstein wrote:
> Code:
>> options("contrasts")
> $contrasts
>            factor           ordered 
> "contr.treatment"      "contr.poly"
> I want to change the first entry ONLY, without retyping "contr.poly". How do
> I do it? I have tried various possibilities and cannot get anything to work.

This doesn't really save typing, but you could do this:

conts <- options("contrasts")$contrasts
conts[1] <- "contr.poly"
options(contrasts = conts)

(One thing I wonder about:  which version of R are you using?  Mine 
shows the names of the contrasts as "unordered" and "ordered".)

> I found out that the response to options("contrasts") has class "list", but
> that doesn't help me, although I think it ought to help.
> Second question (metaquestion). How should I go about finding out the answer
> to a question like "How does one change a single item in a list?"

Read the Introduction to R manual, in particular the bits on replacement 
functions.  The general idea is that you can sometimes index the target 
of an assignment, as I did above.  I could also have written it as

conts <- options("contrasts")
conts$contrasts[1] <- "contr.poly"

One might guess that

options("contrasts")[1] <- "contr.poly"

would work, but one would be wrong.  There is no "options<-" replacement 
function.  (There might be one in some contributed package; I was just 
looking in the base packages.)

Duncan Murdoch

> My answer to the meta-meta-question is to post to this list. I hope that at
> least that part is correct.
> Thanks for any help.
> David Epstein

More information about the R-help mailing list