[R] How to transform: 4 columns into two columns stacked

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Fri Jul 16 14:30:12 CEST 2010


its maybe not as simple as Teds solution but points to a more general
approach

reshape(mydata,direction="long",varying=names(mydata),v.names=c("id","x","y"))

Am 16.07.2010 13:27, schrieb Ralf B:
> I have the following data structure:
>
> n=5
> mydata <- data.frame(id=1:n, x=rnorm(n), y=rnorm(n), id=1:n,
> x=rnorm(n), y=rnorm(n))
> print(mydata)
>
> producing the following represention
>
> id          x           y id.1       x.1        y.1
> 1  1  0.5326855 -2.07633703    1 0.7930274 -1.0530558
> 2  2  0.7888909  0.63354693    2 0.5908323 -1.3543282
> 3  3  0.5350803 -0.20108931    3 2.5079242 -0.4657274
> 4  4 -1.3041960 -0.25195129    4 1.6294046 -1.4094830
> 5  5  0.3109767 -0.02305981    5 0.5183756  1.3084776
>
>
> however I need to transform this data into this form:
>
>     id          x           y
> 1  1  0.5326855 -2.07633703
> 2  2  0.7888909  0.63354693
> 3  3  0.5350803 -0.20108931
> 4  4 -1.3041960 -0.25195129
> 5  5  0.3109767 -0.02305981
> 6  1 0.7930274 -1.0530558
> 7  2 0.5908323 -1.3543282
> 8  3 2.5079242 -0.4657274
> 9  4 1.6294046 -1.4094830
> 10 5 0.5183756  1.3084776
>
> what is the simplest way to do that?
>
> Thanks a lot in advance!
> Ralf
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>   

-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790



More information about the R-help mailing list