route_rolling_diff {stplanr} | R Documentation |
Return smoothed differences between vector values
Description
This function calculates a simple rolling mean in base R. It is useful for calculating route characteristics such as mean distances of segments and changes in gradient.
Usage
route_rolling_diff(x, lag = 1, abs = TRUE)
Arguments
x |
Numeric vector to smooth |
lag |
The window size of the smoothing function. The default, 3, will take the mean of values before, after and including each value. |
abs |
Should the absolute (always positive) change be returned? True by default |
See Also
Other route_funs:
route_average_gradient()
,
route_rolling_average()
,
route_rolling_gradient()
,
route_sequential_dist()
,
route_slope_matrix()
,
route_slope_vector()
Examples
r1 <- od_data_routes[od_data_routes$route_number == 2, ]
y <- r1$elevations
route_rolling_diff(y, lag = 1)
route_rolling_diff(y, lag = 2)
r1$elevations_diff_1 <- route_rolling_diff(y, lag = 1)
r1$elevations_diff_n <- route_rolling_diff(y, lag = 1, abs = FALSE)
d <- cumsum(r1$distances) - r1$distances / 2
diff_above_mean <- r1$elevations_diff_1 + mean(y)
diff_above_mean_n <- r1$elevations_diff_n + mean(y)
plot(c(0, cumsum(r1$distances)), c(y, y[length(y)]), ylim = c(80, 130))
lines(c(0, cumsum(r1$distances)), c(y, y[length(y)]))
points(d, diff_above_mean)
points(d, diff_above_mean_n, col = "blue")
abline(h = mean(y))
[Package stplanr version 1.2.1 Index]