R逻辑问题:较长的对象长度不是较短对象长度的倍数(dplyr::if_else())

R逻辑问题:较长的对象长度不是较短对象长度的倍数(dplyr::if_else()),r,performance,if-statement,dplyr,logical-or,R,Performance,If Statement,Dplyr,Logical Or,我不知道如何使用if\u 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

我不知道如何使用
if\u 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
库(dplyr)
#目标是知道每个记录属于哪个阈值

数据我认为
if_else
不是这里的正确功能。尝试使用
findInterval
cut
,它将找到您的
数据所在的
threshold
的正确存储区

findInterval(data, threshold)
使用
cut

cut(data, threshold)
如果要获取
阈值
索引,请使用
cut
labels=FALSE

cut(data, threshold, labels = FALSE)

数据
阈值
需要具有相同的长度才能进行比较。你想在这里取得什么成就?谢谢你的帮助。你是对的,我使用了你的第一个解决方案。