Skip to content
Prev 305245 / 398506 Next

Maintaining specific order when using aggregate or change order on axis

Hello,

If you order your original (wanted) order and then order that result you 
get what you want.
The example is with fake data but the idea is in the last two lines.


# Make up some data
set.seed(9210)
n <- 1e2
depth <- c('surface', 'shallow', 'deep', 'fmax', 'below')
dat <- data.frame(Depth = sample(depth, n, TRUE), Value = runif(n))

# This is sorted by increasing Depth
agg <- aggregate(Value ~ Depth, data = dat, FUN = mean)

ix <- order(depth)  # First step
agg[order(ix), ]  # inverse permutation

Hope this helps,

Rui Barradas

Em 11-09-2012 18:52, Tinus Sonnekus escreveu: