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
(Ted Harding)
2010-Jul-16 11:56 UTC
[R] How to transform: 4 columns into two columns stacked
On 16-Jul-10 11:27:18, Ralf B wrote:> 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! > RalfSomething on the lines of dataframe(id = c(mydata$id, mydata$id1), x = c(mydata$x , mydata$x1 ), y = c(mydata$y , mydata$y1 ) Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 16-Jul-10 Time: 12:56:48 ------------------------------ XFMail ------------------------------
Eik Vettorazzi
2010-Jul-16 12:30 UTC
[R] How to transform: 4 columns into two columns stacked
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