开发者

Given a vector a=[1,2, 3.2, 4, 5] and an element x=3 In vector a, how to find the exact entry which is bigger than x?

开发者 https://www.devze.com 2023-01-13 07:02 出处:网络
Given a vector a=[1,2, 3.2, 4, 5] and an element x=3 In vector a, how to find the exact entry which is bigger than x? In R开发者_高级运维, is there any function to do that?> a <- c(1,2, 3.2, 4,

Given a vector a=[1,2, 3.2, 4, 5] and an element x=3 In vector a, how to find the exact entry which is bigger than x? In R开发者_高级运维, is there any function to do that?


> a <- c(1,2, 3.2, 4, 5)
> x <- 3
> a[a > x]
[1] 3.2 4.0 5.0
> min(a[a > x])
[1] 3.2


the answer...

 min(a[a>3])


Or the longer one:

which(x < a)
## [1] 3 4 5
which(a > x)
## [1] 3 4 5

As you can see, it returns vector indices.

0

精彩评论

暂无评论...
验证码 换一张
取 消