Skip to content

options("contrasts")

3 messages · David Epstein, Duncan Murdoch, (Ted Harding)

#
Code:
$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.
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?"

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
#
On 26/08/2008 6:30 AM, David Epstein wrote:
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".)
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"
options(conts)

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
#
On 26-Aug-08 10:30:30, David Epstein wrote:
In view of your meta-meta-strategy, here is a response to the
meta-question:

If you sijmply want to replace a given component (say $C) of
a list L, then use code like:

  L$C <- your.replacement

If you want to change the contents of a component of a list,
then what you need to do will depend on the nature of that
component (number, vector, array, anova table, list ... ).
Simple example:

  L<-list(A="A",B="B",C="Z",D="D")
  L
# $A
# [1] "A"
# $B
# [1] "B"
# $C
# [1] "Z"
# $D
# [1] "D"

  C<-L$C  ## extract $C from L
  C
# [1] "Z"
  C<-"C"  ## change it
  L$C<-C  ## put it back
  L
# $A
# [1] "A"
# $B
# [1] "B"
# $C
# [1] "C"
# $D
# [1] "D"
It has been known to work ...
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 26-Aug-08                                       Time: 14:42:29
------------------------------ XFMail ------------------------------