简体   繁体   English

在向量中查找大于 X 的第一个值的位置

[英]Find position of first value greater than X in a vector

在 R 中:我有一个向量,想找到第一个大于 100 的值的位置。

# Randomly generate a suitable vector
set.seed(0)
v <- sample(50:150, size = 50, replace = TRUE)

min(which(v > 100))

Most answers based on which and max are slow (especially for long vectors) as they iterate through the entire vector:大多数基于whichmax答案在遍历整个向量时都很慢(尤其是对于长向量):

  1. x>100 evaluates every value in the vector to see if it matches the condition x>100评估向量中的每个值以查看它是否与条件匹配
  2. which and max / min search all the indexes returned at step 1. and find the maximum/minimum whichmax / min搜索所有在第 1 步返回的索引。并找到最大值/最小值

Position will only evaluate the condition until it encounters the first TRUE value and immediately return the corresponding index, without continuing through the rest of the vector. Position只会评估条件,直到遇到第一个 TRUE 值并立即返回相应的索引,而不会继续遍历向量的其余部分。

# Randomly generate a suitable vector
v <- sample(50:150, size = 50, replace = TRUE)

Position(function(x) x > 100, v)

Check out which.max :查看which.max

x <- seq(1, 150, 3)
which.max(x > 100)
# [1] 35
x[35]
# [1] 103

Just to mention, Hadley Wickham has implemented a function, detect_index , to do exactly this task in his purrr package for functional programming. detect_index提一下,Hadley Wickham 已经实现了一个函数detect_index来在他的用于函数式编程的purrr包中完成这个任务。

I recently used detect_index myself and would recommend it to anyone else with the same problem.我最近自己使用了detect_index并将其推荐给其他有同样问题的人。

Documentation for detect_index can be found here: https://rdrr.io/cran/purrr/man/detect.html可在此处找到检测detect_index文档: https : detect_index

There are many solutions, another is:有很多解决方案,另一个是:

x <- 90:110
which(x > 100)[1]

Assuming values is your vector.假设值是您的向量。

 firstGreatearThan <- NULL
  for(i in seq(along=values)) { 
    if(values[i] > 100) {
       firstGreatearThan <- i
       break
    }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到每个矩阵行的第一个值等于或大于预定义值的 position? - How can I find the position of the first value of every matrixrow that equals or is greater than a pre-defined value? 从R中的两个向量中,找到第二个向量中的min大于第一个中的每个值 - From two vectors in R, find the min in the second vector greater than each value in the first for 循环查找 position 实际值与下一个值之和大于 x - for loop to find position where the sum of the actual value and the next one is greater than x 用于查找值是否大于向量中的所有先前值的函数 - Function to find if a value is greater than all prior values in a vector 查找比当前值大/小 x 的值的第一个出现(行)(遍历数据框中的每一行) - Find the first incidence (row) of a value that is x amount greater/less than the current value (iterated through each row in a data frame) R 查找向量 x 中大于向量 y 中的值的计数 - R find count of values in vector x which are greater than values in vector y 查找大于0的最小值 - Find minimum value greater than 0 找到R向量中第一个非NA值的索引位置? - Find the index position of the first non-NA value in an R vector? 移除向量中大于值的元素 - Remove elements in a vector that are greater than value 如果向量分量之间的时间大于 x,则拆分 dateTime 向量 - Splitting a dateTime vector if time is greater than x between vector components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM