我的目标是将多个函数应用于多个列100,以启用GForce.
假设我有以下数据框
library(data.table)
df <- data.table(fruit = c('a', 'a', 'a', 'b')
, revenue = 1:4
, profit = c(2,NA,4,5)
); df
fruit revenue profit
1: a 1 2
2: a 2 NA
3: a 3 4
4: b 4 5
并且我希望将多个函数应用到多个列(除了fruit
个之外)
# functions
y <- \(i) {c(min(i, na.rm = T)
, max(i, na.rm = T)
)
}
# apply
df[, lapply(.SD, y)
, fruit
, verbose = T
]
Finding groups using forderv ... forder.c received 4 rows and 1 columns
0.000s elapsed (0.000s cpu)
Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu)
lapply optimization changed j from 'lapply(.SD, y)' to 'list(y(revenue), y(profit))'
GForce is on, left j unchanged
Old mean optimization is on, left j unchanged.
Making each group and running j (GForce FALSE) ...
memcpy contiguous groups took 0.000s for 2 groups
eval(j) took 0.012s for 2 calls
0.020s elapsed (0.020s cpu)
fruit revenue profit
1: a 1 2
2: a 3 4
3: b 4 5
4: b 4 5
现在,上面的方法奏效了!
然而,请注意,它显示的是(GForce FALSE)
.所以GForce是101开着的.
我认为这是因为,当使用\(i) sum(i)
时,GForce为Waldi pointed out,GForce为104.
然后我try 了下面的方法,只以lapply
分通过了na.rm = T
分
# functions
z <- \(i) {c(min
, max
)
}
# apply
df[, lapply(.SD, z, na.rm = T)
, fruit
, verbose = T
]
Finding groups using forderv ... forder.c received 4 rows and 1 columns
0.000s elapsed (0.000s cpu)
Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu)
lapply optimization changed j from 'lapply(.SD, z, na.rm = T)' to 'list(z(revenue, na.rm = T), z(profit, na.rm = T))'
GForce is on, left j unchanged
Old mean optimization is on, left j unchanged.
Making each group and running j (GForce FALSE) ... Error in z(revenue, na.rm = T) : unused argument (na.rm = T)
这一次的错误与上面的一样.具体来说,Error in z(revenue, na.rm = T) : unused argument (na.rm = T)
如有任何帮助,将不胜感激