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. Thanks, Ulrich -- __________________________________________________ Ulrich Leopold MSc. Department of Physical Geography Institute for Biodiversity and Ecosystem Dynamics Faculty of Science University of Amsterdam Nieuwe Achtergracht 166 NL-1018WV Amsterdam Phone: +31 20 525 7456 (7451 Secretary) Fax: +31 20 525 7431 Mobile: +31 64 220 3028 Email: uleopold at science.uva.nl URL: www.science.uva.nl/ibed/research/Research_Fields/fg/stafffg/index.html
Is the following what you want? > DF <- data.frame(a=1:2, b=3:4) > DF$a-DF$b [1] -2 -2 > DF[,"a"]-DF[,"b"] [1] -2 -2 hope this helps. spencer graves Ulrich Leopold wrote:> 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. > > Thanks, Ulrich
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 -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On 4 Sep 2003, Ulrich Leopold wrote:> 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.Suppose your data frame is called foo, and you want the first column minus the second: foo[,1] - foo[,2] -- Cheers, Kevin ------------------------------------------------------------------------------ "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." -- Charles Babbage (1791-1871) ---- From Computer Stupidities: http://rinkworks.com/stupid/ -- Ko-Kang Kevin Wang Master of Science (MSc) Student SLC Tutor and Lab Demonstrator Department of Statistics University of Auckland New Zealand Homepage: http://www.stat.auckland.ac.nz/~kwan022 Ph: 373-7599 x88475 (City) x88480 (Tamaki)
Ulrich Leopold
2003-Sep-04 12:41 UTC
[R] subtract 2 columns in a data.frame -- found the problem
Dear list, sorry for having bothering you. I found the problem. It was the a stupid error made by me. R could not recognise the variable as it is of course case sensitive for names. So I specified the wrong variable name and R did complain about it with "numeric(0)". 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 >-- __________________________________________________ Ulrich Leopold MSc. Department of Physical Geography Institute for Biodiversity and Ecosystem Dynamics Faculty of Science University of Amsterdam Nieuwe Achtergracht 166 NL-1018WV Amsterdam Phone: +31 20 525 7456 (7451 Secretary) Fax: +31 20 525 7431 Mobile: +31 64 220 3028 Email: uleopold at science.uva.nl URL: www.science.uva.nl/ibed/research/Research_Fields/fg/stafffg/index.html
Martin Maechler
2003-Sep-04 15:48 UTC
[R] subtract 2 columns in a data.frame -- found the problem
>>>>> "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 >>