R/shiftyears.R

Defines functions shiftyears

Documented in shiftyears

# File shiftyears.R
# Part of the hydroTSM R package, https://github.com/hzambran/hydroTSM ; 
#                                 https://CRAN.R-project.org/package=hydroTSM
# Copyright 2023-2025 Mauricio Zambrano-Bigiarini
# Distributed under GPL 2 or later

################################################################################
# shiftyears: Shifting years backwards for correct computation of annual       # 
#             values when the hydrological year does not start in january      #
################################################################################
# This function shift backwards a vector of dates/times (Date/POSIXct/POSIXlt),#
# by 'lstart.month' (number representing the starting month to be used in the  #
# computation of annual values, by default 1:Jan)                              #
################################################################################
# This is an internal function, just developed to be used by some function that#
# needs to compute annual values with a year starting in a month different     #
# from January.                                                                #
################################################################################
# Output: numeric vector representing the year corresponing to each element of 
#         'ltime', considering 'lstart.month' as the starting month for annual
#         computations
shiftyears <- function(ltime,       # vector with the date/times of each element 
                                    # of a zoo object
                       lstart.month # numeric in [2,..,12], 2:Feb, 12:Dec, 
                                    # It represents the starting month to be 
                                    # used in the computation of annual values.
                       ) {
  syears.bak        <- as.numeric(format( ltime, "%Y" ))
  syears            <- syears.bak
  smonths           <- as.numeric(format( ltime, "%m"))
  months2moveback   <- 1:(lstart.month-1)
  N                 <- length(months2moveback)
  for (i in 1:N) {
    m.index          <- which(smonths == months2moveback[i])
    syears[m.index]  <- syears[m.index] - 1
  } # FOR end

  return(syears)
} # 'shiftyears' END

Try the hydroTSM package in your browser

Any scripts or data that you put into this service are public.

hydroTSM documentation built on April 28, 2026, 9:07 a.m.