[R] add columns to a data.frame

apjaworski@mmm.com apjaworski at mmm.com
Fri Apr 12 18:40:21 CEST 2002


Yes.  Here is one way of doing this:

> zz <- data.frame(x=1:10, y=rnorm(10))
> zz
    x           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
> zz

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



More information about the R-help mailing list