1

我不确定如何使用另一种方法修改我的代码if_else()并保持其效率。这是我原始代码的一个简单示例:

library(dplyr)

# The goal is to know between which threshold belongs each record
data <- c(runif(99999), NA)
threshold <- seq(0, 1, by=0.1)
rank <- if_else(is.na(data), NA, max(which(data >= threshold))) # Error: longer object length is not a multiple of shorter object length

谢谢

4

1 回答 1

1

我认为if_else这里的功能不正确。尝试使用findIntervalor cutwhich 将找到thresholddata所在的正确桶。

findInterval(data, threshold)

cut

cut(data, threshold)

如果要获取索引,请使用cutwithlabels = FALSEthreshold

cut(data, threshold, labels = FALSE)
于 2020-05-23T01:18:59.673 回答