According to the documentation (see ?sapply
), the first argument (X
) to sapply
can be a vector. There is no restriction on the vector saying it has to be of the form 1:x
. So your current code should loop correctly over the values of a
:
sapply(c(a), function(x) b[x,2]-b[x,1])
Note, that you don't need c(a)
- just a
is fine:
sapply(a, function(x) b[x,2]-b[x,1])
If for some reason you did not communicate to us this does not work, it is not a problem with sapply
but with the function you are passing to it.