Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

r - Passing entire data.table to j expression

I want to use all columns as an object in j expression on a grouped data.table (just like tidyverse's group_modify function).

So far, I accomplished this by doing:

dt[, some_function(data.table(.SD)), some_column, .SDcols = colnames(dt)]

But this feels clunky, is there another (simpler) way to do it?

question from:https://stackoverflow.com/questions/65903597/passing-entire-data-table-to-j-expression

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems that there isn't any functions in the package that present this behaviour. The way that makes the code more readable (at least for me) was by creating a wrapper function and use it:

temp_fun <- function(f, ...) {
  f(as.data.table(list(...)))
}

dt[, temp_fun(some_function, .SD), some_column]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...