List order

I came across an application in which I need to sort data.frame by column numbers, and none of the usual solutions seem to allow it.

The context creates the as.data.frame.by method. Because the by object will have its last column as a column of values ​​and the first columns of ncol-1 as index columns. melt returns its sorting back - index 3, then index 2, then index 1. For compatibility with latex.table.by I would like to sort it forward. But I am having problems with this in a fairly general way. The enclosed line in the function below is my best attempt.

 as.data.frame.by <- function( x, colnames=paste("IDX",seq(length(dim(x))),sep="" ), ... ) { num.by.vars <- length(dim(x)) res <- melt(unclass(x)) res <- na.omit(res) colnames(res)[seq(num.by.vars)] <- colnames #res <- res[ order(res[ , seq(num.by.vars)] ) , ] # Sort the results by the by vars in the heirarchy given res } dat <- transform( ChickWeight, Time=cut(Time,3), Chick=cut(as.numeric(Chick),3) ) my.by <- by( dat, with(dat,list(Time,Chick,Diet)), function(x) sum(x$weight) ) > as.data.frame(my.by) IDX1 IDX2 IDX3 value 1 (-0.021,6.99] (0.951,17.3] 1 3475 2 (6.99,14] (0.951,17.3] 1 5969 3 (14,21] (0.951,17.3] 1 8002 4 (-0.021,6.99] (17.3,33.7] 1 640 5 (6.99,14] (17.3,33.7] 1 1596 6 (14,21] (17.3,33.7] 1 2900 13 (-0.021,6.99] (17.3,33.7] 2 2253 14 (6.99,14] (17.3,33.7] 2 4734 15 (14,21] (17.3,33.7] 2 7727 22 (-0.021,6.99] (17.3,33.7] 3 666 23 (6.99,14] (17.3,33.7] 3 1391 24 (14,21] (17.3,33.7] 3 2109 25 (-0.021,6.99] (33.7,50] 3 1647 26 (6.99,14] (33.7,50] 3 3853 27 (14,21] (33.7,50] 3 7488 34 (-0.021,6.99] (33.7,50] 4 2412 35 (6.99,14] (33.7,50] 4 5448 36 (14,21] (33.7,50] 4 8101 

If the line is uncommented, it returns gibberish (it simply considers the entire data file. Frame as a vector, with disastrous results).

I even tried smart things like res <- res[ order( ...=list(res[,1],res[,2]) ) , ] but to no avail.

I suspect there is an easy way to do this, but I don't see it.

Edit for clarification . I want to not specify column names. Instead, I want to be able to sort it by a numerical vector (for example, sort by columns 1: 4).

+7
source share
1 answer
 mydf <- as.data.frame(my.by) mydf[order(mydf$IDX3, mydf$IDX2, mydf$IDX1) , ] IDX1 IDX2 IDX3 value 1 (-0.021,6.99] (0.951,17.3] 1 3475 3 (14,21] (0.951,17.3] 1 8002 2 (6.99,14] (0.951,17.3] 1 5969 4 (-0.021,6.99] (17.3,33.7] 1 640 6 (14,21] (17.3,33.7] 1 2900 5 (6.99,14] (17.3,33.7] 1 1596 13 (-0.021,6.99] (17.3,33.7] 2 2253 15 (14,21] (17.3,33.7] 2 7727 14 (6.99,14] (17.3,33.7] 2 4734 22 (-0.021,6.99] (17.3,33.7] 3 666 24 (14,21] (17.3,33.7] 3 2109 23 (6.99,14] (17.3,33.7] 3 1391 25 (-0.021,6.99] (33.7,50] 3 1647 27 (14,21] (33.7,50] 3 7488 26 (6.99,14] (33.7,50] 3 3853 34 (-0.021,6.99] (33.7,50] 4 2412 36 (14,21] (33.7,50] 4 8101 35 (6.99,14] (33.7,50] 4 5448 

Or;

 my.by <- by( dat, with(dat,list(Diet,Chick, Time)), function(x) sum(x$weight) ) mydf <- as.data.frame(my.by) 

EDIT: Or this produces the same output as the top using column numeric indices:

  mydf <- as.data.frame(my.by) mydf[ do.call(order, mydf[, 3:1] ) , ] 
+7
source

All Articles