I am currently working with daily, relative snow cover values (per km2) in a large dataset of rougly 40 k km2 over 20 years and would like to get the days at which the snow cover increases within a certain period. The measurements are taken once a day and thus, it is possible that relative snowcover increases / decreases over several days.
However, I struggle to count / indicate the number of times my daily values decrease / increase compared to the previous days. I created some fake (but possible) data and calculated the differences between element n and element n + 1:
set.seed(134)
c <- c(sort(runif(6, 0, 1), decreasing = FALSE),
sort(runif(10, 0, 1), decreasing = TRUE),
sort(runif(4, 0, 1), decreasing = FALSE),
0.9, 0.9, 0.9, sort(runif(3, 0, 1), decreasing = TRUE),
sort(runif(8, 0, 1), decreasing = FALSE)) # create fake snow cover fraction data
plot(c)
cdiff <- c [1:length(c) - 1] - c [2:length(c)]
I then thought of using sign to indicate the algebraic sign
> sign(cdiff)
[1] 1 1 1 1 1 1 0 1 -1 1 1 1 1 1 1 1 1 1 1 -1 -1 -1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1
I now have two problems:
1.) How to get the location of the new vector at which the algebraic sign changes
2.) Even values can either be found between (i) two positives, (ii) two negatives or (iii) a mixture. In my example, I would like to treat same values as part of the sub vectors around these values (in this case: positive signs but decreasing snow cover fraction).
The solution I aim for is receiving a vector with the days at which the values turn positive compared to the previous day again (in this case: c(10,21,26)
)
The solution does not have to include sign()
, it's just what I thought may bring me closer to solving the problem.
Thanks in advance!