[R] How to duplicate rows in dataframe?

james.holtman@convergys.com james.holtman at convergys.com
Mon Dec 13 21:22:59 CET 2004





> x.1 <- data.frame(a=1:5, b=1:5)
> x.1
  a b
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
> x.1[c(1,2,2,2,3,3,4,4,5,4,3,2,1),]
    a b
1   1 1
2   2 2
2.1 2 2
2.2 2 2
3   3 3
3.1 3 3
4   4 4
4.1 4 4
5   5 5
4.2 4 4
3.2 3 3
2.3 2 2
1.1 1 1
>
__________________________________________________________
James Holtman        "What is the problem you are trying to solve?"
Executive Technical Consultant  --  Office of Technology, Convergys
james.holtman at convergys.com
+1 (513) 723-2929


                                                                                                                                           
                      cstrato                                                                                                              
                      <cstrato at aon.at>             To:       r-help at stat.math.ethz.ch                                                      
                      Sent by:                     cc:                                                                                     
                      r-help-bounces at stat.m        Subject:  [R] How to duplicate rows in dataframe?                                       
                      ath.ethz.ch                                                                                                          
                                                                                                                                           
                                                                                                                                           
                      12/13/2004 14:02                                                                                                     
                                                                                                                                           
                                                                                                                                           




Dear all:

I have the following (simple?) problem:
Consider a dataframe where the first column contains
integers used as index, e.g.
    index
     24
     13
     46
     32

Now I have the following vector used to sort the dataframe:
    x <- c(13,24,32,46)
Sorting the dataframe can be done by using order.

However consider the following vector:
    x <- c(13,32,13,24,46,24,24)
Now I want to get the dataframe in the order of the rows
defined in x, i.e. the dataframe contains duplicate rows.
One way to achieve this would be to use rbind in a for-loop.

My question is:
Is there an easier and - more important - faster way to
obtain the dataframe as defined in x?

Thank you in advance.
Best regards
Christian
_._._._._._._._._._._._._._._._
C.h.i.s.t.i.a.n S.t.r.a.t.o.w.a
V.i.e.n.n.a       A.u.s.t.r.i.a
_._._._._._._._._._._._._._._._

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list