在函数中按名称传递data.table列

时间:2016-04-26 17:51:49

标签: r data.table

我想将列名传递给函数并使用列索引和setorder函数:

require(data.table)
data(iris)

top3 = function(t, n) {
  setorder(t, n, order=-1)
  return ( t[1:3, .(Species, n)])
}

DT = data.table(iris)
top3(DT, Petal.Width)

但是,这会返回错误:

Error in setorderv(x, cols, order, na.last) : some columns are not in the data.table: n,1

我认为我误解了如何在R中传递裸列名称。我有什么选择?

2 个答案:

答案 0 :(得分:7)

你可以做到

top3 = function(DT, nm) eval(substitute( DT[order(-nm), .(Species, nm)][, head(.SD, 3L)] ))
top3(DT, Petal.Width)

     Species Petal.Width
1: virginica         2.5
2: virginica         2.5
3: virginica         2.5

我建议在函数内反对(1)setorder,因为它有副作用; (2)当你在未来少于三行的data.table上使用1:3进行索引时,会产生奇怪的效果; (3)修复3而不是使其成为函数的参数; (4)使用n作为名称...仅仅是我个人倾向于保留n作为计数。

答案 1 :(得分:2)

假设您的数据集总是有超过3行并且这是您要在该数据表上执行的唯一操作,那么使用setorderv可能符合您的利益。

top3 = function(t, n) {
  setorderv(t, n, -1)
  return ( t[1:3, c("Species", n), with=FALSE])
}

DT = data.table(iris)
top3(DT, "Petal.Width")

结果:

     Species Petal.Width
1: virginica         2.5
2: virginica         2.5
3: virginica         2.5
相关问题