R Count number of sign switches for adjacent vector elements -
Consider this vector:
-3 4-5 -2 4-56 -1 -1 -18 -8 7 How many "signature switch" are present for the elements of the edge, to calculate what is a simple way in R (eg without loop) ?
In this example we will have 7 switches:
-3 to 4-5 to -2 -2 to 4 4 to -5 -5 to 6 6 to -1 - 8 to 7
Here is a function that does the trick:
# Seeing a numerical array X, this gives the number of sign changes nignigns & lt; - function (x) {signal & lt; - sign (x) sum (sign [-1]! = Sign [-length (x)])} a & lt; - c (-3, 4, 5, -2, 4, -5, 6, -1, -1, -1, -8, 7) N signage (A)
Comments
Post a Comment