Size of a predefined interval within a vector

2

I would like to know how I can calculate the amount of elements I have in a predefined range, within a vector.

For example: Let's say I have a vector of size 10. My intention for example is to want to know how many elements have between the second element of my vector up to the sixth element of my vector.

vetor=c(1,2,3,4,5,6,7,8,9,10)

I want to know how many numbers I have between the second element of my vector ( vetor[2] ) and the sixth element ( vetor[6] ). Which in this case are 5 elements.

NOTE: In this case I want the second and the sixth element to be part of the count.

    
asked by anonymous 26.12.2014 / 02:22

1 answer

2

Well, as Luiz Vieira said, in this case you can simply calculate.

Anyway, just so you do not leave without a response, you can make the subset of your vector first vetor[2:6] and then count how many elements the subset of the vector has using length :

vetor=c(1,2,3,4,5,6,7,8,9,10) 
length(vetor[2:6])
[1] 5
    
26.12.2014 / 03:25