【发布时间】:2020-11-03 23:26:01
【问题描述】:
我有一个阈值向量,我想用它在 data.table 上创建列的 bin
thrshlds <- seq(from = 0, to = 1, by = 0.05)
test <- data.table(
A = rnorm(1000, 0.7, 1),
B = rbinom(1000, 3, 0.6)
)
我要实现的逻辑是:
如果 A 列的值等于或小于每个阈值的值,则为其分配相应的阈值。类似于 SQL case when,但无需手动分配每个阈值。
类似:
test[, new_category := fcase(A <= thrshlds[1], thrshlds[1],
A <= thrshlds[2], thrshlds[2],
.....)]
但我不知道如何在 data.table 查询中进行这种迭代。
谢谢!
【问题讨论】:
标签: r data.table