[R] subtract 2 columns in a data.frame -- found the problem

Martin Maechler maechler at stat.math.ethz.ch
Thu Sep 4 17:48:18 CEST 2003


>>>>> "Ulrich" == Ulrich Leopold <uleopold at science.uva.nl>
>>>>>     on Thu, 04 Sep 2003 14:41:41 +0200 writes:

    Ulrich> Dear list, sorry for having bothering you. I found
    Ulrich> the problem. It was the a stupid error made by me. R
    Ulrich> could not recognise the variable as it is of course
    Ulrich> case sensitive for names. So I specified the wrong
    Ulrich> variable name and R did complain about it with
    Ulrich> "numeric(0)".

    Ulrich> Ulrich

Exactly for reasons like these, S language teachers (not all AFAIK) have
recommended to use the slightly more cumbersome

 x[,"<varname>"]  instead of
 x $ <varname>

for things like these: 
With the [,".."] form, you immediately get an (intelligible)
error message when you mistype the variable name.

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><



    Ulrich> Peter Dalgaard BSA wrote:
    >> Ulrich Leopold <uleopold at science.uva.nl> writes:
    >> 
    >> 
    >>> Dear list,
    >>> 
    >>> could someone point me to the right command to subtract
    >>> 2 columns in a data.frame. Might be a bit embarrassing
    >>> question. But I cannot figure out how to do this simple
    >>> command in R.
    >> 
    >> 
    >> E.g.,
    >> 
    >> mydata$difference <- mydata$x - mydata$y
    >>




More information about the R-help mailing list