简体   繁体   English

从另一个向量中查找向量的索引

[英]Finding the index of a vector from another vector

I have three vectors a, r and b . 我有三个向量a, r and b I want to check the first element of r is between zero and first element of vector a , if yes then put zero from vector b , if not , its belong to what interval and then extract the value form vector b , ...How can I do this in R?at the end I should have a vector with sample 20 and the values belong to vector b . 我想检查r的第一个元素是否在向量a第一个元素和第一个元素之间,如果是,则从向量b放入零,如果不是,则它属于什么区间然后从值中提取向量b ,...怎么能我在R?中做到这一点,我应该有一个样本20的向量,值属于向量b

      a =c(0.1531532, 0.2432432, 0.3243243, 0.3648649, 0.3873874, 0.4054054, 
       0.4594595, 0.5720721, 0.7522523, 1.0000000)
      set.seed(2)
      r = runif(20, 0, 1)
      b = 0:9
      result = c( 1, 8, 8, 1, 9, 9, 0, 9, 7, 7, 7, 1, 9, 1, 5, 9, 9, 1, 6, 0)
R> a = c(0, a)
R> cut(r, a, labels = b)
 [1] 1 8 8 1 9 9 0 9 7 7 7 1 9 1 5 9 9 1 6 0
Levels: 0 1 2 3 4 5 6 7 8 9

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

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