Skip to content

Commit e388086

Browse files
committed
added custom windown to ma8h
1 parent 1356e90 commit e388086

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

R/ma8h.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#' @param data data.frame with time column and variable columns to be processed
66
#' @param time name of the time column (default is date) in POSIXct
77
#' @param var name of the columns to be calculated
8+
#' @param n custom time window (n = 8 for 8-hour average)
89
#' @param verbose display additional information
910
#' @param ... parameters passed to hourly
1011
#'
@@ -26,16 +27,16 @@
2627
#' pch = 19,
2728
#' legend = c('hourly','8h-mov average'),
2829
#' col = c('black','blue'))
29-
ma8h <- function(data, time = 'date', var, verbose = TRUE, ...) {
30+
ma8h <- function(data, time = 'date', var, n = 8, verbose = TRUE, ...) {
3031

3132
if(!'hourly' %in% class(data)){
3233
data <- hourly(data,time = time, verbose = verbose, ...)
3334
}
3435

3536
if(verbose)
36-
cat('processing maximum daily 8h average ... \n')
37+
cat(paste0('processing ',n,'-hour moving average ... \n'))
3738

38-
moving_average <- function(x, n = 8) {
39+
moving_average <- function(x, n = n) {
3940
if (n > length(x)) {
4041
stop("Window size 'n' should be less than or equal to the length of the vector 'x'.") # nocov
4142
}
@@ -52,11 +53,9 @@ ma8h <- function(data, time = 'date', var, verbose = TRUE, ...) {
5253
}
5354
var <- VARS[1]
5455
data$roll_avg <- moving_average(data[[var]], n = 8)
55-
# data$roll_avg <- terra::roll(data[[var]],8,mean,'around',na.rm=T)
5656
output <- data[,c(time,'roll_avg')]
5757
for(var in VARS[-1]){
5858
data$roll_avg <- moving_average(data[[var]], n = 8)
59-
# data$roll_avg <- terra::roll(data[[var]],8,mean,'around',na.rm=T)
6059
output <- cbind(output,new_var = data$roll_avg)
6160
}
6261
names(output) <- c(time,VARS)

man/ma8h.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)