I want to pass the column name of the function and use column indexing and the setorder function:
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)
However, this returns an error:
Error in setorderv(x, cols, order, na.last) : some columns are not in the data.table: n,1
I think I donβt understand how the missing column names work in R. What are my options?
source share