3

I have been having trouble phrasing this question, so if anyone can edit it up to standard that would be great.

I have a vector that looks like this:

 x <- c(1, 2, 5)

How do i return the last index where the difference between the value of the vector in that position and the position is = 0.

In this case, I would like to have

 2

as the difference between the value of the vector and its position for the third element is > 0

 x[3]-3.

As a side note, this is part of a larger function, where the vector 'x' was built as a vector of values that satisfy a condition (being outside of a range). In this example, the vector 'x' was built as the indexes of the vector

 y <- c(1, -0.544099347708607, 0.0330854828196116, 0.126862586350202, -0.189999318205021, 0.0709946572904202, -0.0290039765997793, 0.12201693346217, -0.120410983904152, 0.0974094609584081, -0.119147919464352, 0.0154264136176002, 0.115102403861495, -0.145980255860186, 0.116998886386955, -0.137041816761002, 0.114352714471954, 0.0228895094121642, -0.0679735427311049, 0.0350071153004831, -0.0145366468920295)

Which are outside of the range (-.18, .18)

 plot.ts(y)
 abline(h = 0.18)
 abline(h = -0.18)
1
  • 2
    If x is an increasing sequence of integers starting at 1, just find the first gap: which(diff(x)!=1)
    – Frank
    May 19, 2015 at 17:46

4 Answers 4

4

You can use the Position function:

Position(function(x) {x == 0}, x - 1:length(x), right=T)

See http://stat.ethz.ch/R-manual/R-devel/library/base/html/funprog.html for more functions.

Or as @Frank said below,

Position(`!`, x - 1:length(x), right=T)

This is because 0 is falsey and other numbers are truthy.

4
  • 2
    Fancier: use `!` as the function.
    – Frank
    May 19, 2015 at 17:43
  • 1
    Hm, just looked into Position (which this answer introduced me to) and see that it can be quite slow if the index is far away from where we start searching (in this case, from the right since right=TRUE). Try x <- c(1,3:1e7);system.time(Position(`!`,x-seq_along(x),right=T)); system.time(tail(which(!(x-seq_along(x))),1)). This is because Position is applying `!` sequentially to each element of x-seq_along(x). I guess Position is defined for use with more complicated predicate functions, that take longer to evaluate.
    – Frank
    May 19, 2015 at 19:02
  • @Frank: Mmm, interesting - I guess which is faster because it's implemented in the R interpreter?
    – zw324
    May 19, 2015 at 19:21
  • You can look at the inside of it by just typing Position in. It is faster when (i) the predicate function f is complicated and/or un-vectorizable and (ii) the match shows up early in the search along the vector, I think. Try myfun <- function(x){for(i in 1:1e6){x <- x};TRUE}; system.time(Position(myfun,1:10)); system.time(head(which(sapply(1:10,myfun)),1)).
    – Frank
    May 19, 2015 at 19:28
3

I think the simplest approach is to test equality, not test for the difference being zero:

tail(which(x==seq_along(x)),1)
# 2
3

Here is another approach:

index <- 1:length(x)
max(which(x - index == 0))
#[1] 2

Or as the other Frank points out, you could test for equality instead of the difference being 0.

max(which(x == index))
3
  • which always gives its result in sorted order, so tail makes a little more sense than max. Surprisingly, I see only a small difference in speed (like 15% extra time): x <- 1:5e8; system.time(max(which(x==seq_along(x)))); system.time(tail(which(x==seq_along(x)),1))
    – Frank
    May 19, 2015 at 18:31
  • 1
    @Frank Good point! I just posted what came to mind first. I'll leave it in order to avoid duplicating your answer. Out of curiosity, is it better practice to use seq_along(x) over 1:length(x)? When I compared the two (using microbenchmark) on x<-1:5e8 the difference was negligible.
    – Jota
    May 19, 2015 at 20:41
  • 1
    Yeah, seems about the same to me; I've just picked up the habit here on SO. I've seen people use seq_len(length(x)) which is no better in terms of performance either. These have the advantage of robustness to x having length zero (compare seq_along(NULL);1:length(NULL)), but in most applications that is obviously not possible. I guess it's some sort of developer mindset, like they would want their function to be robust to arbitrary vector input.
    – Frank
    May 19, 2015 at 20:57
0

One can also try this

tail(which((1:length(x)-x)==0),1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.