zhihuali
2008-Sep-22 23:54 UTC
[R] sort a data matrix by all the values and keep the names
Dear all, If I have a data frame x<-data.frame(x1=c(1,7),x2=c(4,6),x3=c(8,2)): x1 x2 x3 1 4 8 7 6 2 I want to sort the whole data and get this: x1 1 x3 2 x2 4 x2 6 x1 7 x3 8 If I do sort(X), R reports: Error in order(list(x1 = c(1, 7), x2 = c(4, 6), x3 = c(8, 2)), decreasing = FALSE) : unimplemented type 'list' in 'orderVector1' The only way I can sort all the data is by converting it to a matrix:> sort(as.matrix(x))[1] 1 2 4 6 7 8 But now I lost all the names attributes. Is it possible to sort a data frame and keep all the names? Thanks! Zhihua Li _________________________________________________________________ [[elided Hotmail spam]] [[alternative HTML version deleted]]
Moshe Olshansky
2008-Sep-23 00:40 UTC
[R] sort a data matrix by all the values and keep the names
One possibility is:> x <- data.frame(x1=c(1,7),x2=c(4,6),x3=c(8,2)) > names <- t(matrix(rep(names(x),times=nrow(x)),nrow=ncol(x))) > m <- as.matrix(x) > ind <- order(m) > df <- data.frame(name=names[ind],value=m[ind]) > dfname value 1 x1 1 2 x3 2 3 x2 4 4 x2 6 5 x1 7 6 x3 8 --- On Tue, 23/9/08, zhihuali <lzhtom at hotmail.com> wrote:> From: zhihuali <lzhtom at hotmail.com> > Subject: [R] sort a data matrix by all the values and keep the names > To: r-help at stat.math.ethz.ch > Received: Tuesday, 23 September, 2008, 9:54 AM > Dear all, > > If I have a data frame > x<-data.frame(x1=c(1,7),x2=c(4,6),x3=c(8,2)): > x1 x2 x3 > 1 4 8 > 7 6 2 > > I want to sort the whole data and get this: > x1 1 > x3 2 > x2 4 > x2 6 > x1 7 > x3 8 > > If I do sort(X), R reports: > Error in order(list(x1 = c(1, 7), x2 = c(4, 6), x3 = c(8, > 2)), decreasing = FALSE) : > unimplemented type 'list' in > 'orderVector1' > > The only way I can sort all the data is by converting it to > a matrix: > > sort(as.matrix(x)) > [1] 1 2 4 6 7 8 > > But now I lost all the names attributes. > > Is it possible to sort a data frame and keep all the names? > > Thanks! > > Zhihua Li > > _________________________________________________________________ > [[elided Hotmail spam]] > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
zhihuali
2008-Sep-23 00:43 UTC
[R] sort a data matrix by all the values and keep the names
This is exactly what I wanted! Thank you so much! Z> Date: Mon, 22 Sep 2008 19:21:43 -0500 > From: markleeds@verizon.net > Subject: RE: [R] sort a data matrix by all the values and keep the names > To: lzhtom@hotmail.com > > Hi: there might be a quicker way but you can use stack and order. stack > creates a dataframe with 2 columns, values and ind, with ind > being the associate columns. > > order(temp$values) creates the indices of the ordered values so you > index by that to make it sorted. > > temp <- stack(x) > print(temp) > print(str(temp)) > > sortedx <- temp[order(temp$values),] > print(sortedx) > > > > On Mon, Sep 22, 2008 at 7:54 PM, zhihuali wrote: > > > Dear all, > > > > If I have a data frame x<-data.frame(x1=c(1,7),x2=c(4,6),x3=c(8,2)): > > x1 x2 x3 > > 1 4 8 > > 7 6 2 > > > > I want to sort the whole data and get this: > > x1 1 > > x3 2 > > x2 4 > > x2 6 > > x1 7 > > x3 8 > > > > If I do sort(X), R reports: > > Error in order(list(x1 = c(1, 7), x2 = c(4, 6), x3 = c(8, 2)), > > decreasing = FALSE) : unimplemented type 'list' in 'orderVector1' > > > > The only way I can sort all the data is by converting it to a matrix: > >> sort(as.matrix(x)) > > [1] 1 2 4 6 7 8 > > > > But now I lost all the names attributes. > > > > Is it possible to sort a data frame and keep all the names? > > > > Thanks! > > > > Zhihua Li > > > > _________________________________________________________________ > > [[elided Hotmail spam]] > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@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._________________________________________________________________ [[elided Hotmail spam]] [[alternative HTML version deleted]]
hadley wickham
2008-Sep-23 00:47 UTC
[R] sort a data matrix by all the values and keep the names
On Mon, Sep 22, 2008 at 6:54 PM, zhihuali <lzhtom at hotmail.com> wrote:> > Dear all, > > If I have a data frame x<-data.frame(x1=c(1,7),x2=c(4,6),x3=c(8,2)): > x1 x2 x3 > 1 4 8 > 7 6 2 > > I want to sort the whole data and get this: > x1 1 > x3 2 > x2 4 > x2 6 > x1 7 > x3 8 > > If I do sort(X), R reports: > Error in order(list(x1 = c(1, 7), x2 = c(4, 6), x3 = c(8, 2)), decreasing = FALSE) : > unimplemented type 'list' in 'orderVector1' > > The only way I can sort all the data is by converting it to a matrix: >> sort(as.matrix(x)) > [1] 1 2 4 6 7 8 > > But now I lost all the names attributes. > > Is it possible to sort a data frame and keep all the names?Here's one way: dfm <- melt(x, id = c()) dfm[order(dfm$value), ] Hadley -- http://had.co.nz/
Steven McKinney
2008-Sep-23 01:02 UTC
[R] sort a data matrix by all the values and keep the names
Is something missing in the melt()?> x<-data.frame(x1=c(1,7),x2=c(4,6),x3=c(8,2)) > require("reshape")Loading required package: reshape> dfm <- melt(x, id = c())Error in if (!missing(id.var) && !(id.var %in% varnames)) { : missing value where TRUE/FALSE needed> dfm[order(dfm$value), ]Error: object "dfm" not found> xx1 x2 x3 1 1 4 8 2 7 6 2> melt(x, id = c())Error in if (!missing(id.var) && !(id.var %in% varnames)) { : missing value where TRUE/FALSE needed>Steve McKinney -----Original Message----- From: r-help-bounces at r-project.org on behalf of hadley wickham Sent: Mon 9/22/2008 5:47 PM To: zhihuali Cc: r-help at stat.math.ethz.ch Subject: Re: [R] sort a data matrix by all the values and keep the names On Mon, Sep 22, 2008 at 6:54 PM, zhihuali <lzhtom at hotmail.com> wrote:> > Dear all, > > If I have a data frame x<-data.frame(x1=c(1,7),x2=c(4,6),x3=c(8,2)): > x1 x2 x3 > 1 4 8 > 7 6 2 > > I want to sort the whole data and get this: > x1 1 > x3 2 > x2 4 > x2 6 > x1 7 > x3 8 > > If I do sort(X), R reports: > Error in order(list(x1 = c(1, 7), x2 = c(4, 6), x3 = c(8, 2)), decreasing = FALSE) : > unimplemented type 'list' in 'orderVector1' > > The only way I can sort all the data is by converting it to a matrix: >> sort(as.matrix(x)) > [1] 1 2 4 6 7 8 > > But now I lost all the names attributes. > > Is it possible to sort a data frame and keep all the names?Here's one way: dfm <- melt(x, id = c()) dfm[order(dfm$value), ] Hadley -- http://had.co.nz/ ______________________________________________ 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.
Possibly Parallel Threads
- An R clause to bind dataframes under certain contions
- Comparison of aggregate in R and group by in mysql
- locate the rows in a dataframe with some criteria
- how to change a dataframe with characters to a numeric matrix?
- how to use a function in aggregate which accepts matrix and outputs matrix?