Obtain a moving average for monthly, annual or seasonal resolution data from monthly map data.
get_ma(x, type, season = NULL, size = 10, format = "table", use_mclapply = FALSE, mc.cores = 32)
x | a data frame (or data table) or a list of these. |
---|---|
type | character, one of |
season |
|
size | number of years for the moving average window. Default |
format | return results as a single data frame with |
use_mclapply | use |
mc.cores | number of CPUs for parallel processing when |
returns a single data frame or a list of data frames.
get_ma
takes a single data frame or a list of data frames.
A list is useful for example if the table is very large and is pre-split into a list of smaller data frames for parallel processing on a Linux cluster with many CPU cores available.
If x
is a data frame rather than a data table, it will be converted to a data table.
For each data frame get_ma
computes and returns the moving or rolling average,
after first summarizing monthly data to seasonal or annual averages if applicable.
Winter begins in December and each season is three consecutive months (DJF, MAM, JJA, SON).
Whether x
is a data frame or list of data frames, the output can be returned as a single data frame or list of data frames
Parallel processing is Linux-only (uses mclapply
) and the default of 32 CPU cores will have to be changed if you don't have that many.
It is convenient for my usage and this package version is not intended for general use.
Most other users will probably not be using parallel processing at all, in which case n.cores
is ignored.
data(monthlytemps) res <- "seasonal" # annual, seasonal, monthly season <- "winter" # winter, spring, summer, autumn idx <- switch(season, winter=c(12,1,2), spring=3:5, summer=6:8, autumn=9:11) if(res=="seasonal") monthlytemps <- dplyr::filter(monthlytemps, Month %in% idx) get_ma(monthlytemps, res, season)#> # A tibble: 80 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2015 0.526 #> 2 -177. 53.7 2016 0.410 #> 3 -177. 53.7 2017 0.522 #> 4 -177. 53.7 2018 0.460 #> 5 -177. 53.7 2019 0.547 #> 6 -177. 53.7 2020 0.477 #> 7 -177. 53.7 2021 0.598 #> 8 -177. 53.7 2022 0.702 #> 9 -177. 53.7 2023 0.665 #> 10 -177. 53.7 2024 0.713 #> # ... with 70 more rowsget_ma(list(monthlytemps, monthlytemps), res, season)#> # A tibble: 160 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2015 0.526 #> 2 -177. 53.7 2016 0.410 #> 3 -177. 53.7 2017 0.522 #> 4 -177. 53.7 2018 0.460 #> 5 -177. 53.7 2019 0.547 #> 6 -177. 53.7 2020 0.477 #> 7 -177. 53.7 2021 0.598 #> 8 -177. 53.7 2022 0.702 #> 9 -177. 53.7 2023 0.665 #> 10 -177. 53.7 2024 0.713 #> # ... with 150 more rowsget_ma(monthlytemps, res, season, format="list")#> $`2015` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2015 0.526 #> #> $`2016` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2016 0.410 #> #> $`2017` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2017 0.522 #> #> $`2018` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2018 0.460 #> #> $`2019` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2019 0.547 #> #> $`2020` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2020 0.477 #> #> $`2021` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2021 0.598 #> #> $`2022` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2022 0.702 #> #> $`2023` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2023 0.665 #> #> $`2024` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2024 0.713 #> #> $`2025` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2025 0.668 #> #> $`2026` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2026 0.826 #> #> $`2027` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2027 0.869 #> #> $`2028` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2028 0.937 #> #> $`2029` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2029 0.963 #> #> $`2030` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2030 1.04 #> #> $`2031` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2031 1.07 #> #> $`2032` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2032 0.980 #> #> $`2033` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2033 0.992 #> #> $`2034` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2034 0.942 #> #> $`2035` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2035 1.09 #> #> $`2036` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2036 0.995 #> #> $`2037` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2037 0.992 #> #> $`2038` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2038 0.989 #> #> $`2039` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2039 0.936 #> #> $`2040` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2040 0.886 #> #> $`2041` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2041 0.852 #> #> $`2042` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2042 0.960 #> #> $`2043` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2043 1.01 #> #> $`2044` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2044 1.03 #> #> $`2045` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2045 0.98 #> #> $`2046` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2046 1.04 #> #> $`2047` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2047 1.18 #> #> $`2048` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2048 1.18 #> #> $`2049` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2049 1.28 #> #> $`2050` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2050 1.24 #> #> $`2051` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2051 1.25 #> #> $`2052` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2052 1.28 #> #> $`2053` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2053 1.43 #> #> $`2054` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2054 1.54 #> #> $`2055` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2055 1.52 #> #> $`2056` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2056 1.55 #> #> $`2057` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2057 1.45 #> #> $`2058` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2058 1.52 #> #> $`2059` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2059 1.50 #> #> $`2060` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2060 1.65 #> #> $`2061` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2061 1.72 #> #> $`2062` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2062 1.79 #> #> $`2063` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2063 1.74 #> #> $`2064` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2064 1.82 #> #> $`2065` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2065 1.94 #> #> $`2066` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2066 2.08 #> #> $`2067` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2067 2.14 #> #> $`2068` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2068 2.16 #> #> $`2069` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2069 2.14 #> #> $`2070` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2070 2.09 #> #> $`2071` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2071 2.01 #> #> $`2072` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2072 1.98 #> #> $`2073` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2073 2.01 #> #> $`2074` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2074 1.89 #> #> $`2075` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2075 1.77 #> #> $`2076` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2076 1.65 #> #> $`2077` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2077 1.60 #> #> $`2078` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2078 1.57 #> #> $`2079` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2079 1.71 #> #> $`2080` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2080 1.77 #> #> $`2081` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2081 1.83 #> #> $`2082` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2082 1.80 #> #> $`2083` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2083 1.79 #> #> $`2084` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2084 1.84 #> #> $`2085` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2085 1.95 #> #> $`2086` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2086 1.98 #> #> $`2087` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2087 1.95 #> #> $`2088` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2088 1.97 #> #> $`2089` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2089 1.82 #> #> $`2090` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2090 1.76 #> #> $`2091` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2091 1.77 #> #> $`2092` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2092 1.84 #> #> $`2093` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2093 1.79 #> #> $`2094` #> # A tibble: 1 x 4 #> lon lat Year z #> <dbl> <dbl> <dbl> <dbl> #> 1 -177. 53.7 2094 1.82 #># NOT RUN { get_ma(list(monthlytemps, monthlytemps), res, season, use_mclapply=T, mc.cores=2) # }