[R] Extract indices after comparing two vectors

Phil Spector spector at stat.berkeley.edu
Tue Apr 19 01:52:46 CEST 2011


?which

> A <- c(1,2,3,4)
> B  <- c(0,3,1,5)
> which(A<B)
> max(which(A<B))
[1] 4

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Mon, 18 Apr 2011, Chee Chen wrote:

> Dear All,
> I would like to ask a question about how to locate the indices after comparing two numeric vectors.
> Say, I have,  A <- c(1,2,3,4) and  A <- c(0,3,1,5)
> and we cmpare:
>>  idx <- A<B
> So:
>> idx
> [1] FALSE  TRUE FALSE  TRUE
>
> Question 1:
> I would like to get a vector that stores the indices for entries of A such that this relationship is true. In this case,
> a vector  idx1 < c(2,4), because the 2nd and 4th entries of A satisfy the conditions
>
> Question 2:
> I would like to know the maximum index of the entry in A for which the relationship is true. In this case,
> max_indx = 4, since the 4th entry of A is such.
>
> Could you please give me some hints?
>
> Thank you,
> -Chee
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list