I have a data frame where in each column is the value of events in the period, I need to calculate the increment and decrement of these periods for a larger base and I could not do:
p1<- rep(2:11)
p2<- rep(3:12)
p3<- rep(1:10)
p4<- rep(4:13)
base<- cbind(p1,p2,p3,p4)
base
p1 p2 p3 p4
[1,] 2 3 1 4
[2,] 3 4 2 5
[3,] 4 5 3 6
[4,] 5 6 4 7
[5,] 6 7 5 8
[6,] 7 8 6 9
[7,] 8 9 7 10
[8,] 9 10 8 11
[9,] 10 11 9 12
[10,] 11 12 10 13
Expected:
p1 p2 p3 p4 in dc
[1,] 2 3 1 4 2 1
[2,] 3 4 2 5 2 1
[3,] 4 5 3 6 2 1
[4,] 5 6 4 7 2 1
[5,] 6 7 5 8 2 1
[6,] 7 8 6 9 2 1
[7,] 8 9 7 10 2 1
[8,] 9 10 8 11 2 1
[9,] 10 11 9 12 2 1
[10,] 11 12 10 13 2 1
That is, taking as an example line 9:
[9,] 10 11 9 12 2 1
comparing p2 with p1 there was an increase (10> 11) for period 3 there was a decrease (11 > 9) for period 4 there was another increment (9 > 12) totaling 2 increments and a decrease.
The idea is to execute this for a set of 500 variables viewed in 10 periods.