如何包装“在数据表中降序排序”成一个函数? [英] How to wrap "descending sort in data table" into a function?
本文介绍了如何包装“在数据表中降序排序”成一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在查看在data.table中降序排序和速度后, / p>
我想换行
X < order(Year,MemberID,-Month)]
或
X [,Month:= - Month]
setkey(X,Year,MemberID,Month)
X [,Month:
进入一个函数,如 d.setkey(data,key) code>
但是,似乎 order
和:=
rhs只接受列名而不是字符,我不知道如何传递参数。
解决方案
使用 get
:
DT [,Month:= (Month),with = TRUE]
或:
b $ b
DT [,`:=`(Month= - get(Month))]
或更常见的使用表达式:
expr < replace(x:= -x,list(x = as.name(Month)))
DT [,eval(expr)]
After seeing descending sort in data.table and speed
I would like to wrap either
X <- X[order(Year, MemberID, -Month)]
or
X[,Month:=-Month]
setkey(X,Year,MemberID,Month)
X[,Month:=-Month]
Into a function, like d.setkey(data, key)
However, seems order
and :=
rhs only accept column names instead of character, I don't know how I can pass argument?
解决方案
You can use get
:
DT[, "Month" := -get("Month"),with=TRUE]
Or :
DT[,`:=`("Month"=-get("Month"))]
Or more general using an expression:
expr <- substitute(x := -x, list(x=as.name("Month")))
DT[,eval(expr)]
这篇关于如何包装“在数据表中降序排序”成一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文