Skip data.table column by name in function

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?

+5
source share
2 answers

You can do

 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 

I would advise (1) a setorder inside a function, since it has side effects; (2) indexing with 1:3 , when you can use this on a data table with less than three rows in the future, to a strange effect; (3) fixing 3 instead of making it an argument to a function; and (4) using n for the name ... just my personal preference to reserve n for counting.

+7
source

Assuming your dataset will always contain more than 3 rows and that this is ONLY the operation you want to perform in this data table, you might need to use 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") 

Result:

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

Source: https://habr.com/ru/post/1247978/


All Articles