[英]sort data.table after grouping
I have a data like this 我有这样的数据
z = data.table(name =sample(letters[1:6],10,replace=T),
Var1=rnorm(10,1,2),
Var2 =runif(10,1,10))
I want sum the var1by name and sort the sum like the fake code 我想要对var1by名称求和,并像伪代码那样对总和进行排序
z[order(sum(Var1)),sum(Var1),by =name]
But the code above doesn't work,any insights? 但上面的代码不起作用,任何见解?
This can be done in a two step process - ie 1) create a sum
column grouped by 'name' then order
based on the new column ('V1') and assign that column to NULL (if the intention is to order the whole dataset) 这可以在两个步骤的过程来完成-即1)创建sum
由“名称”则分组列order
基于新的柱(“V1”)和指定该列进行NULL(如果目的是订购整个数据集)
z[, V1 := sum(Var1) , by = name][order(V1)][, V1 := NULL][]
If we are only ordering the summarized output 如果我们只是订购汇总输出
z[, sum(Var1) , by = name][order(V1)]
Or as @Frank suggested set
functions will be more efficient 或者@Frank建议的set
功能将更有效
z[, V1 := sum(Var1) , by = name]
setorder(z, V1)[, V1 := NULL]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.