[R-gui] Lengthening a data frame by repeating it

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jan 30 14:32:31 CET 2009


In the post x and y are matrices, not data frames.  Converting them to
data frames, try:

> x.df <- as.data.frame(x); y.df <- as.data.frame(y)
> data.frame(x.df[1], x.df[2], y.df)
  V1 V2 V1.1 V2.1
1  1  3   11   21
2  2  4   12   22
3  1  3   13   23
4  2  4   14   24
5  1  3   15   25
6  2  4   16   26


On Fri, Jan 30, 2009 at 8:21 AM, Yannis Tzamouranis
<yannis_tzamouranis at yahoo.com> wrote:
> Hello,
>
> this initially seemed to me like a pretty simple thing to do with rbind/cbind but did not manage to do it without looping.
>
> I have a simple data frame such as x:
> x<-cbind(c(1,2),c(3,4))
>> x
>      [,1] [,2]
> [1,]    1    3
> [2,]    2    4
>
> And a longer data frame, such as y:
>> y<-cbind(c(11,12,13,14,15,16),c(21,22,23,24,25,26))
>> y
>      [,1] [,2]
> [1,]   11   21
> [2,]   12   22
> [3,]   13   23
> [4,]   14   24
> [5,]   15   25
> [6,]   16   26
>
> I would like to merge them like this:
>      [,1] [,2] [,3] [,4]
> [1,]    1    3   11   21
> [2,]    2    4   12   22
> [3,]    1    3   13   23
> [4,]    2    4   14   24
> [5,]    1    3   15   25
> [6,]    2    4   16   26
>
> (ie by repeating the short frame as many times as necessary (a known number) and inserting it before the y columns.
> The need to not loop comes from the large number of times it would have to be repeated(thousands) before merged with another table of the longer, new length.
>
> Any ideas?
>
> Thank you,
> Yannis Tzamouranis
>
> I tried lengthening x (as then merging them would be easy through cbind) but could not do it without looping.  I wonder if there would be another way to go about this...
>
>        [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-SIG-GUI mailing list
> R-SIG-GUI at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-gui
>
>



More information about the R-SIG-GUI mailing list