Dear R community: it is possible to add a column wich results from an algebraic combination of columns from a data frame to the same data frame? e.g I have a data frame like $X: num[1:10] 10,20,25,20... $Y: num[1:10] 152,458,698,587,... and I want to add a new column like $Z where Z=X+5*Y what should I do? Thanks in advance Juan Pablo _________________________________________________________________ Hable con sus amigos en l?nea, pruebe MSN Messenger: http://messenger.msn.es -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 12.4.2002 17:37 Uhr, juan pablo perez wrote:> it is possible to add a column wich results from an algebraic combination of > columns from a data frame to the same data frame? > > e.g > > I have a data frame like > $X: num[1:10] 10,20,25,20... > $Y: num[1:10] 152,458,698,587,... > and I want to add a new column like > $Z where Z=X+5*Y > > what should I do?Just this (assuming your data frame is called "data"): data$Z <- data$X + 5 * data$Y The new column is created automatically. You may want to read the "Introduction to R" manual that came with the software. This is quite a basic way to use data frames. Cheers Kaspar Pflugshaupt -- Kaspar Pflugshaupt Geobotanisches Institut Zuerichbergstr. 38 CH-8044 Zuerich Tel. ++41 1 632 43 19 Fax ++41 1 632 12 15 mailto:pflugshaupt at geobot.umnw.ethz.ch privat:pflugshaupt at mails.ch http://www.geobot.umnw.ethz.ch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Yes. Here is one way of doing this:> zz <- data.frame(x=1:10, y=rnorm(10)) > zzx y 1 1 0.43665715 2 2 -0.23219549 3 3 0.20102028 4 4 0.85460148 5 5 1.83858972 6 6 0.83336441 7 7 -0.01167367 8 8 -0.08473429 9 9 0.52357340 10 10 0.23190303> zz$z <- zz$x*10+zz$y > zzx y z 1 1 0.43665715 10.43666 2 2 -0.23219549 19.76780 3 3 0.20102028 30.20102 4 4 0.85460148 40.85460 5 5 1.83858972 51.83859 6 6 0.83336441 60.83336 7 7 -0.01167367 69.98833 8 8 -0.08473429 79.91527 9 9 0.52357340 90.52357 10 10 0.23190303 100.23190 Cheers, Andy __________________________________ Andy Jaworski Engineering Systems Technology Center 3M Center, 518-1-01 St. Paul, MN 55144-1000 ----- E-mail: apjaworski at mmm.com Tel: (651) 733-6092 Fax: (651) 736-3122 "juan pablo perez" To: r-help at stat.math.ethz.ch <valdentro at hotm cc: (bcc: Andrzej P. Jaworski/US-Corporate/3M/US) ail.com> Subject: [R] add columns to a data.frame 04/12/2002 10:37 Dear R community: it is possible to add a column wich results from an algebraic combination of columns from a data frame to the same data frame? e.g I have a data frame like $X: num[1:10] 10,20,25,20... $Y: num[1:10] 152,458,698,587,... and I want to add a new column like $Z where Z=X+5*Y what should I do? Thanks in advance Juan Pablo _________________________________________________________________ Hable con sus amigos en l¨ªnea, pruebe MSN Messenger: http://messenger.msn.es -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
(Reply to juan pablo perez)> Dear R community: > > it is possible to add a column wich results from an algebraic combination of > columns from a data frame to the same data frame? > > e.g > > I have a data frame like > $X: num[1:10] 10,20,25,20... > $Y: num[1:10] 152,458,698,587,... > and I want to add a new column like > $Z where Z=X+5*Y > > what should I do?Use cbind.data.frame Christoph. -- Christoph Lange clange at epost.de Verhaltensbiologie, FU Berlin 838-55068 Haderslebener Str. 9, 12163 Berlin http://www.verhaltensbiologie.fu-berlin.de/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._