Description
library(data.table)
irisDT <- data.table(iris)
foo = function(w) {
bv = "Species"
groupingsets(
irisDT,
j = lapply(.SD, \(y) sum(y > w)),
by = bv,
sets = as.list(bv),
.SDcols = c("Sepal.Length", "Petal.Length")
) |> print()
irisDT[, lapply(.SD, \(y) sum(y > w)), .SDcols = c("Sepal.Length", "Petal.Length"), by=bv]
}
foo(5)
# Error in ..FUN1(Sepal.Length) : object 'w' not found
w = 4
foo(5)
# Species Sepal.Length Petal.Length
# <fctr> <int> <int>
# 1: setosa 50 0
# 2: versicolor 50 34
# 3: virginica 50 50
# Species Sepal.Length Petal.Length
# <fctr> <int> <int>
# 1: setosa 22 0
# 2: versicolor 47 1
# 3: virginica 49 41
I'm using data.table ‘1.14.7’ (just updated with data.table::update_dev_pkg()
) with R 4.2.1 on Windows 10.