From: Martin Maechler <[EMAIL PROTECTED]>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-ID: <[EMAIL PROTECTED]>
Date: Fri, 3 Nov 2006 22:26:20 +0100
To: Barry Rowlingson <[EMAIL PROTECTED]>
Cc: Sarah Goslee <[EMAIL PROTECTED]>,
    [email protected],
    Sarah White <[EMAIL PROTECTED]>
Subject: Re: [R] Translation of R code required
In-Reply-To: <[EMAIL PROTECTED]>
References: <[EMAIL PROTECTED]>
        <[EMAIL PROTECTED]>
        <[EMAIL PROTECTED]>
        <[EMAIL PROTECTED]>
        <[EMAIL PROTECTED]>
X-Mailer: VM 7.19 under Emacs 21.4.1
Reply-To: Martin Maechler <[EMAIL PROTECTED]>

>>>>> "BaRow" == Barry Rowlingson <[EMAIL PROTECTED]>
>>>>>     on Fri, 03 Nov 2006 18:38:38 +0000 writes:

    BaRow> Sarah Goslee wrote:
    >> Since this step works,
    >> 
    >> mental$Rx <- factor(mental$Rx, levels=c("VS","IPS"))
    >> 
    >> I think that some of the names are right, but perhaps
    >> that one is spelled differently (Centre vs centre, maybe,
    >> since R is case-sensitive?).

    BaRow>   How do you know that step works? If the dataframe
    BaRow> 'mental' doesnt have an 'Rx' column then mental$Rx
    BaRow> will be 'NULL', and the factor() function will make a
    BaRow> factor out of that...

    >> x=data.frame(foo=1:10)

    BaRow>   so x is a data frame with one column, called
    BaRow> 'foo'. Now lets make levels from a non-existent
    BaRow> column, 'bar':

    >> x$bar=factor(x$bar,levels=c('x','y'))

    BaRow>   No complaints... But try printing 'x' now (by
    BaRow> typing x at the command line)... Ick!

Hmm, what version of R would that be?

In any recent versions,
I get

  > x <- data.frame(foo = 1:10)
  > x$bar <- factor(x$bar,levels=c('x','y'))
  Error in as.vector(x, mode) : invalid argument 'mode'

But I want to make another point:

For about a year now, for "serious" data-analysis using data frames,
I've been advocating to use the slightly more clumsy but much
more error-prone ``column indexing by name'' instead of the quick-and-dirty
"$" selection :

  > x$bar
  NULL

  > x[,"bar"]
  Error in "[.data.frame"(x, , "bar") : undefined columns selected


    BaRow>   Try things one line at a time and check the objects
    BaRow> created or modified are sensible.

Indeed!

______________________________________________
[email protected] 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.

Reply via email to