I was searching in the internet for a R function that can help me find the interval of index in which vector is increasing, decreasing and remains constant.
For example, I have a following vector representing a pollutant's concentration. At some interval, it is increasing and at some it is decreasing. There are 274 indexes in this vector.
My question is how can I find the interval of index of this vector in which it is increasing/decreasing/remaining constant?
Thank you so much for your time.
my_vector <- c(726.3239,723.9239,721.5239,719.1239,716.7239,720.4239,724.1239,727.8239,731.5239,735.2239,738.9239,742.6239,746.3239,750.0239,753.7239,755.4239,757.1239,758.8239,760.5239,762.2239,763.9239,765.6239,767.3239,769.0239,770.7239,771.0239,771.3239,771.6239,771.9239,772.2239,772.5239,772.8239,773.1239,773.4239,773.7239,775.4239,777.1239,778.8239,780.5239,782.2239,783.9239,785.6239,787.3239,789.0239,790.7239,791.0239,791.3239,791.6239,791.9239,792.2239,792.5239,792.8239,793.1239,793.4239,793.7239,793.9239,794.1239,794.3239,794.5239,794.7239,794.9239,795.1239,795.3239,795.5239,795.7239,796.5239,797.3239,798.1239,798.9239,799.7239,800.5239,801.3239,802.1239,802.9239,803.7239,802.4239,801.1239,799.8239,798.5239,797.2239,795.9239,794.6239,793.3239,792.0239,790.7239,789.4239,788.1239,786.8239,785.5239,784.2239,782.9239,781.6239,780.3239,779.0239,777.7239,777.6239,777.5239,777.4239,777.3239,777.2239,777.1239,777.0239,776.9239,776.8239,776.7239,777.8239,778.9239,780.0239,781.1239,782.2239,783.3239,784.4239,785.5239,786.6239,787.7239,789.0239,790.3239,791.6239,792.9239,794.2239,795.5239,796.8239,798.1239,799.4239,800.7239,801.0239,801.3239,801.6239,801.9239,802.2239,802.5239,802.8239,803.1239,803.4239,803.7239,802.7239,801.7239,800.7239,799.7239,798.7239,797.7239,796.7239,795.7239,794.7239,793.7239,792.2239,790.7239,789.2239,787.7239,786.2239,784.7239,783.2239,781.7239,780.2239,778.7239,778.6239,778.5239,778.4239,778.3239,778.2239,778.1239,778.0239,777.9239,777.8239,777.7239,777.5239,777.3239,777.1239,776.9239,776.7239,776.5239,776.3239,776.1239,775.9239,775.7239,775.4239,775.1239,774.8239,774.5239,774.2239,773.9239,773.6239,773.3239,773.0239,772.7239,772.2239,771.7239,771.2239,770.7239,770.2239,769.7239,769.2239,768.7239,768.2239,767.7239,766.0239,764.3239,762.6239,760.9239,759.2239,757.5239,755.8239,754.1239,752.4239,750.7239,750.5239,750.3239,750.1239,749.9239,749.7239,749.5239,749.3239,749.1239,748.9239,748.7239,743.3239,737.9239,732.5239,727.1239,721.7239,716.3239,710.9239,705.5239,700.1239,694.7239,690.4239,686.1239,681.8239,677.5239,673.2239,668.9239,664.6239,660.3239,656.0239,651.7239,647.0239,642.3239,637.6239,632.9239,628.2239,623.5239,618.8239,614.1239,609.4239,604.7239,588.3239,571.9239,555.5239,539.1239,522.7239,506.3239,489.9239,473.5239,457.1239,440.7239,436.5239,432.3239,428.1239,423.9239,419.7239,415.5239,411.3239,407.1239,402.9239,398.7239,396.0239,393.3239,390.6239,387.9239,385.2239,382.5239,379.8239,377.1239,374.4239)
CodePudding user response:
You can use the base R diff
, sign
and which
functions to identify the element pairs with sign change differences:
x <- my_vector
z1 <- diff(x)
z2 <- sign(z1)
z3 <- diff(z2)
no_change <- which(z3 == 0)
no_change
minus_change <- which(z3 < 0)
minus_change
[1] 74 134
plus_change <- which(z3 > 0)
plus_change
[1] 4 104
In this case there are no zero sign changes. Note that for a vector of length n, the diff and sign vectors contain n-1 elements. So for example the minus_change values of 74, 134 represent the sign differences of the x[75:76] and x[135:136] pairs. See the help info for the R functions.