[R] Manipulating dataframe

james.holtman@convergys.com james.holtman at convergys.com
Fri Nov 1 22:55:05 CET 2002


Here is an example:

> x.1
  id x1 x2  x3
1  1  1 10 100
2  2  2 20 200
3  3  3 30 300
4  4  4 40 400
5  5  5 50 500
> x.2 <- data.frame(id=rep(x.1$id,rep(3,nrow(x.1))),
+     x1=c(x.1$x1,rep(NA,nrow(x.1)*2)),
+     x2=c(rep(NA,nrow(x.1)), x.1$x2, rep(NA,nrow(x.1))),
+     x3=c(rep(NA,nrow(x.1)*2), x.1$x3),
+     xrep=c(x.1$x1,x.1$x2,x.1$x3))
> x.2
   id x1 x2  x3 xrep
1   1  1 NA  NA    1
2   1  2 NA  NA    2
3   1  3 NA  NA    3
4   2  4 NA  NA    4
5   2  5 NA  NA    5
6   2 NA 10  NA   10
7   3 NA 20  NA   20
8   3 NA 30  NA   30
9   3 NA 40  NA   40
10  4 NA 50  NA   50
11  4 NA NA 100  100
12  4 NA NA 200  200
13  5 NA NA 300  300
14  5 NA NA 400  400
15  5 NA NA 500  500
>


                                                                                                     
                    Ravi Varadhan                                                                    
                    <rvaradha at jhsph.edu>       To:     r-help at stat.math.ethz.ch                      
                    Sent by:                   cc:                                                   
                    owner-r-help at stat.ma       Subject:     [R] Manipulating dataframe               
                    th.ethz.ch                                                                       
                                                                                                     
                                                                                                     
                    11/01/2002 15:16                                                                 
                                                                                                     
                                                                                                     




Hi:

Suppose, I have a dataframe (with N rows) containing the following
variables:

ID  X82  X85  X88

>From this, I'd like to create a new dataframe (with 3*N rows), which
will have the following variables:

ID  X82  X85  X88  Xrep

where: each ID is duplicated 3 times; X82 equals its original value for
the first row and equals NA for the other two; X85 equals its original
value for the second row and equals NA for the other two; X88 equals
its original value for the third row and equals NA for the other two;
Xrep will equal X82, X85 and X88 in the first, second and third rows,
respectively.

Is there an efficient way to do this?

Thanks for any help,
Ravi.


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




--
"NOTICE:  The information contained in this electronic mail transmission is
intended by Convergys Corporation for the use of the named individual or
entity to which it is directed and may contain information that is
privileged or otherwise confidential.  If you have received this electronic
mail transmission in error, please delete it from your system without
copying or forwarding it, and notify the sender of the error by reply email
or by telephone (collect), so that the sender's address records can be
corrected."

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