简体   繁体   English

在数组中查找特定值

[英]Finding specific values in an array

I am relatively new to R programming. 我对R编程比较陌生。 I am writing a code that generates an array of numbers: 我正在编写一个生成数字数组的代码:

[1] 0.5077399,  0.4388107, 0.3858783, 0.3462711, 0.3170844, 0.2954411, 0.2789464, 0.2658839,

[9] 0.2551246, 0.2459498 

Note: I manually separated the values by commas for ease on the eyes :) 注意:为了方便起见,我用逗号手动分隔了值 :)

I want to pick the first 3 numbers from this array that are below 0.3 - [0.2954411, 0.2658839, 0.2551246] . 我想从此数组中选择前三个数字,它们在0.3 - [0.2954411, 0.2658839, 0.2551246] In addition to picking these values, I want to generate the numbers that represents where those three values exist within the array. 除了选择这些值之外,我还想生成表示这三个值在数组中的位置的数字。 In this case, I want the code to give me [6,7,8]. 在这种情况下,我想要代码给我[6,7,8]。

How would I write code to do this? 我将如何编写代码来做到这一点?

I greatly appreciate the help. 我非常感谢您的帮助。

For a similar simulated set, 对于类似的模拟集,

y <- c(2, 4,6, 8)
ind <- which(y < 6) ## for finding indices 1 and 2
val <- y[y<6]       ## for picking values  2 and 4

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM