有多少偶数索引值大于前面的值?

How many even-indexed values are greater than the preceding value?

我想知道 A[even number] 的计数大于 A[even number-1]

>A
[1] 1 2 3 1 4 5 5 6 7

我的以下代码无法运行:

for (i in 1:length(A)){
  coun<-0
  if (length(A)%%2!=0){
    len <- length(A)-1
    for (i in 1: len){
      if (A[i+1]>A[len]){
        coun<-coun+1
      }
    }
  else if (A[i+1]>A[i]){
    coun<-coun+1  
}
}

在 A 中,计数应为 3,因为 (A[2]>A[1],A[4]A[5],A[8]> A[7],A[9] 不会被考虑,因为它后面没有任何价值)

几种方式:

## find the even indices and
## calculate the differences you want
even_ind = seq_along(A)[c(F, T)]
sum(A[even_ind] > A[even_ind - 1])
# [1] 3


## or, calculate all differences, 
## and take every other one
sum((diff(A) > 0)[c(T, F)])
# [1] 3