order - R Plyr - Ordering results from DDPLY? -
does know slick way order results coming out of ddply summarise operation?
this i'm doing output ordered descending depth.
ddims <- ddply(diamonds, .(color), summarise, depth = mean(depth), table = mean(table)) ddims <- ddims[order(-ddims$depth),]
with output...
> ddims color depth table 7 j 61.88722 57.81239 6 61.84639 57.57728 5 h 61.83685 57.51781 4 g 61.75711 57.28863 1 d 61.69813 57.40459 3 f 61.69458 57.43354 2 e 61.66209 57.49120
not ugly, i'm hoping way nicely within ddply(). know how?
hadley's ggplot2 book has example ddply , subset it's not sorting output, selecting 2 smallest diamonds per group.
ddply(diamonds, .(color), subset, order(carat) <= 2)
i'll use occasion advertise bit data.table
, faster run , (in perception) @ least elegant write:
library(data.table) ddims <- data.table(diamonds) system.time(ddims <- ddims[, list(depth=mean(depth), table=mean(table)), by=color][order(depth)]) user system elapsed 0.003 0.000 0.004
by contrast, without ordering, ddply
code takes 30 times longer:
user system elapsed 0.106 0.010 0.119
with respect have hadley's excellent work, e.g. on ggplot2
, , general awesomeness, must confess me, data.table
entirely replaced ddply
-- speed reasons.
Comments
Post a Comment