Hello,
I very general question but probably usefull to others as well:
Is there any prebuild function that reorders a dataframe from:
x1 x2
1 100 200
2 101 201
3 102 202
4 103 203
5 104 204
6 105 205
to
1 100 x1
2 101 x1
3 102 x1
4 103 x1
5 104 x1
6 105 x1
7 200 x2
8 201 x2
9 202 x2
10 203 x2
11 204 x2
12 205 x2
I found a way with:
names <- rep(c("x1","x2"),c(length(x1),length(x2)))
x <-c(x1,x2)
data.frame(x,names)
but probably there is already another function for doing such things more
easily.
Thank you
Johannes
--
df = data.frame(x1 = 100:105, x2 = 200:205) library(reshape2) melt(df) Michael On Wed, Nov 9, 2011 at 10:14 AM, Johannes Radinger <JRadinger at gmx.at> wrote:> Hello, > > I very general question but probably usefull to others as well: > > Is there any prebuild function that reorders a dataframe from: > > ? x1 ? ?x2 > 1 ?100 ? 200 > 2 ?101 ? 201 > 3 102 202 > 4 ?103 ? 203 > 5 ?104 ? 204 > 6 ?105 ? 205 > > to > > 1 ?100 ?x1 > 2 ?101 ?x1 > 3 ?102 ?x1 > 4 ?103 ?x1 > 5 ?104 ?x1 > 6 ?105 ?x1 > 7 ?200 ?x2 > 8 ?201 ?x2 > 9 ?202 ?x2 > 10 ?203 ?x2 > 11 ?204 ?x2 > 12 ?205 ?x2 > > I found a way with: > > names <- rep(c("x1","x2"),c(length(x1),length(x2))) > x <-c(x1,x2) > data.frame(x,names) > > but probably there is already another function for doing such things more easily. > > Thank you > Johannes > -- > > ______________________________________________ > 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. >
On Nov 9, 2011, at 10:14 AM, Johannes Radinger wrote:> Hello, > > I very general question but probably usefull to others as well: > > Is there any prebuild function that reorders a dataframe from: > > x1 x2 > 1 100 200 > 2 101 201 > 3 102 202 > 4 103 203 > 5 104 204 > 6 105 205 > > to > > 1 100 x1 > 2 101 x1 > 3 102 x1 > 4 103 x1 > 5 104 x1 > 6 105 x1 > 7 200 x2 > 8 201 x2 > 9 202 x2 > 10 203 x2 > 11 204 x2 > 12 205 x2?stack> > I found a way with: > > names <- rep(c("x1","x2"),c(length(x1),length(x2))) > x <-c(x1,x2) > data.frame(x,names)That is basically what stack does.> > but probably there is already another function for doing such things > more easily. > > Thank you > Johannes > -- > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT