r - Use Order on Aggregate(in a single statement) -


i have dataframe below:

  b 1 1 2 2 1 3 3 2 3 4 3 5 5 3 6  

and applying aggregate data below

> aggregate(b ~ a, mydf, sum)    b 1 1  5 2 2  3 3 3 11 

now, want order result based on b, applied order function as:

> aggregate(b ~ a, mydf[order(b),], sum) 

which didn't work. , applied

> aggregate(b ~ a, mydf, sum)[order(b),] 

which didn't work either

how should use order desired result.

as said in comments, can't call b unless tell r b , when from. people using attach (don't this) in order make life easier , call columns without being bothered using $, though in case fail because creating temporary data set, while attached b come unaggregated data set.

attach(mydf) aggregate(b ~ a, mydf, sum)[order(b),] #        b # 1     1  5 # 2     2  3 # 3     3 11 # na   na na # na.1 na na detach(mydf) 

so quit fooling around, base r can mentioned in comment , posted later in above answer.

res <- aggregate(b ~ a, mydf, sum)   res[order(res$b), ] 

but won't in 1 statement , i'm not aware of way of doing otherwise.

though, fortunately have packages can achieve in 1 call.

first, data.table package (in devel version on gh) can achieve both , efficiently using setorder function within same call

# library(devtools) # install_github("rdatatable/data.table", build_vignettes = false) library(data.table) ## v >= 1.9.5 res <- setorder(aggregate(b ~ a, mydf, sum), b) 

though, if @ it, correct data.table syntax be

res <- setorder(setdt(mydf)[, .(b = sum(b)), = a], b) 

an alternative approach using piping combined dplyr package in order achieve same goal in 1 chain of actions

library(dplyr) mydf %>%   group_by(a) %>%   summarise(b = sum(b)) %>%   arrange(b) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -