[R] How to transform: 4 columns into two columns stacked
(Ted Harding)
Ted.Harding at manchester.ac.uk
Fri Jul 16 13:56:52 CEST 2010
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!
> Ralf
Something 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 ------------------------------
More information about the R-help
mailing list