[R] add columns to a data.frame

Kaspar Pflugshaupt pflugshaupt at geobot.umnw.ethz.ch
Fri Apr 12 18:08:52 CEST 2002


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list