First commit with the code Hector shared with me

This commit is contained in:
2026-09-28 17:05:38 +02:00
parent 1f4317d330
commit f3b28ea2ea
190 changed files with 221495 additions and 2 deletions

26
10_EXPORTS/lag_apply.R Normal file
View File

@@ -0,0 +1,26 @@
# rolling
# x = vector of values
# n = window size
# callback = function target, in the format "function(x){sd(x)}" or "function(x){mean(x)}"
'# 04 / 11 /2025
ℹ In group 1: `uniqueID = "PYR11_BASE_01"`.
Caused by error in `x[i:(i + n - 1)]`:
! only 0s may be mixed with negative subscripts
Added: '
lag_apply <- function(x, n, callback){
k = length(x);
result = rep(NA, k);
for(i in 1 : (k - n + 1)){ # 1 : (k - n + 1)
result[i+(n-1)] <- callback(x[i:(i + n-1)]);
}
return(result);
}
# lag_apply(subset(mean_stand_export_list, PLOT == "PYR00")$nTrees.ha, 20, function(x){mean(x)})