A variety of small utilities used in other functions.

Arguments

d

A numeric matrix or data frame with depth in the first column and observations for some variable in each of the remaining columns.

na.rm

Should missing data be removed?

seas

An integer vector of seasons to be retained.

sub

An integer vector.

w

A vector of class "Date".

x

A numeric vector.

x1

A matrix or vector time series.

y

A vector of class "Date" or "POSIX" date-time.

Details

date2decyear: Converts object of class "Date" to decimal year assuming time of day is noon.

decyear2date: Converts decimal year to object of class "Date".

layerMean: Acts on a matrix or data frame with depth in the first column and observations for different variables (or different sites, or different times) in each of the remaining columns. The trapezoidal mean over the given depths is calculated for each of the variables. Replicate depths are averaged, and missing values or data with only one unique depth are handled. Data are not extrapolated to cover missing values at the top or bottom of the layer. The result can differ markedly from the simple mean even for equal spacing of depths, because the top and bottom values are weighted by 0.5 in a trapezoidal mean.

leapYear: TRUE if x is a leap year, FALSE otherwise.

meanSub: Mean of a subset of a vector.

monthNum: Converts dates to the corresponding numeric month.

tsSub: Drops seasons from a matrix or vector time series.

years: Converts dates to the corresponding numeric years.

Examples


dates <- as.Date(c("1996-01-01", "1999-12-31", "2004-02-29", "2005-03-01"))
date2decyear(dates)
#> [1] 1996.001 1999.999 2004.163 2005.163

decyear2date(c(1996.0014, 1999.9986, 2004.1626, 2005.1630))
#> [1] "1996-01-01" "1999-12-31" "2004-02-29" "2005-03-01"

z <- c(1, 2, 3, 5, 10)  # 5 depths
x <- matrix(rnorm(30), nrow = 5)  # 6 variables at 5 depths
layerMean(cbind(z, x))
#>         V1         V2         V3         V4         V5         V6 
#> -0.2851353 -0.3533402 -0.1086362 -0.2130134  0.6604103 -0.8536951 

leapYear(seq(1500, 2000, 100))
#> [1] FALSE  TRUE FALSE FALSE FALSE  TRUE
leapYear(c(1996.9, 1997))
#> [1]  TRUE FALSE

## Aggregate monthly time series over Feb-Apr only.
aggregate(sfbayChla, 1, meanSub, sub = 2:4)
#> Time Series:
#> Start = 1978 
#> End = 2008 
#> Frequency = 1 
#>            s21       s22       s23       s24       s25       s26       s27
#> 1978  3.233333  3.266667  2.733333  3.720833  2.883333  7.000000  3.683333
#> 1979  2.155556  2.433333  2.500000  2.488889       NaN  3.266667  2.655556
#> 1980  2.561111  2.500000  2.116667  1.843333       NaN  5.150000  4.727778
#> 1981       NaN       NaN       NaN  3.077778  3.634444  3.821111  4.265556
#> 1982       NaN       NaN  3.068406  3.658446  5.690825  6.372037  6.776759
#> 1983  4.653241  3.488889  5.247619  4.467063  6.171429  7.944444  8.260185
#> 1984  4.388889  4.055556  4.277778  4.081481  5.011111  5.800000  5.183333
#> 1985       NaN       NaN       NaN       NaN       NaN       NaN       NaN
#> 1986 10.153704  6.822222  7.791667  7.334444  8.803333 10.722222 10.219259
#> 1987  2.766667  2.446667  2.606667  2.716667  2.790000  3.513333  4.133333
#> 1988       NaN  3.386667  3.646667  4.411111  4.866667  3.606667  5.393333
#> 1989  6.273333  5.171190  6.448571  6.465000  8.228095  8.610238  8.306667
#> 1990  3.175556  2.693333       NaN       NaN       NaN       NaN       NaN
#> 1991  3.265556  2.959444  3.223889  3.653651  4.290635  4.397143  4.574286
#> 1992  3.490000  4.217778  4.744444  5.126667  5.502222  6.265556  6.252778
#> 1993  5.633333  4.733333  6.965000  7.136111  9.879444 10.626667 11.231111
#> 1994  3.078000  2.860667  4.414222  3.341333  3.778667  5.846222  6.618222
#> 1995  8.481250  7.969306  9.659444  9.003333 11.801111 15.707111 14.466111
#> 1996  4.370000  3.311111  4.013333  6.153333  7.110000  4.268333  6.419444
#> 1997  5.566667  5.462222  6.473333  6.219444  8.284444  8.761667 10.260000
#> 1998  7.364889  6.097333  6.803889  7.308222  9.810111 10.622778 13.498556
#> 1999  7.093333  6.743333  7.520000  7.420000  7.633333  9.893333  9.010000
#> 2000  8.203333  6.086111  5.258333  6.400000  6.923056  9.253333  8.633333
#> 2001 11.632222  9.690556 11.002778 13.425556 16.873889 14.610000 14.870556
#> 2002        NA        NA        NA        NA        NA        NA        NA
#> 2003 13.281852 13.122222 14.325556 15.310000 17.641111 19.235000 22.005833
#> 2004  6.730000  6.061111  7.070000  7.135556  8.498889  9.101111  9.486667
#> 2005  5.830000  4.726667  6.013333  4.576667  5.543333  5.606667  5.873333
#> 2006 18.064444  9.764444 11.970000 12.451111 12.804444 16.178889 18.130000
#> 2007  7.884444  6.583333  7.786667  7.880000  7.878889  9.237778 10.136667
#> 2008 15.074444 10.948889 12.485556 13.802222 14.096667 14.777778 15.935556
#>            s28       s29       s30       s31       s32       s33       s34
#> 1978  3.966667  4.250000  3.463889  4.783333  6.069444       NaN       NaN
#> 1979  2.933333  4.033333  3.800000  4.766667  5.066667  6.400000  7.333333
#> 1980  6.333333  8.033333  8.366667 16.933333 11.016667 12.550000 11.983333
#> 1981  4.583333  4.942222  5.671111       NaN       NaN       NaN       NaN
#> 1982  8.571481  9.211279 11.989057       NaN       NaN       NaN       NaN
#> 1983 10.760317 11.466667 10.410714 12.572222 11.900926       NaN       NaN
#> 1984  6.733333  7.011111  7.617593  7.066667  8.927778 13.225000       NaN
#> 1985       NaN       NaN  7.592130  8.711111  9.624074       NaN       NaN
#> 1986 12.754444 15.471667 15.156111 18.333333 17.968333       NaN       NaN
#> 1987  4.673333  5.333333  6.373333  7.060000  7.346667       NaN       NaN
#> 1988       NaN       NaN       NaN       NaN 21.163333       NaN       NaN
#> 1989  8.353333  9.245238 11.700952 13.171190 14.658571       NaN       NaN
#> 1990       NaN       NaN       NaN       NaN       NaN       NaN       NaN
#> 1991  4.644365  4.620000  5.055000  4.750556  5.068889  5.156667       NaN
#> 1992  5.743111  5.896889  5.396000  5.080889  4.986000  5.060667  4.804667
#> 1993 12.022222 12.548889 12.299444 12.916667 13.889444 15.714722 15.742917
#> 1994  7.075556  8.615111  8.838667  8.775778 12.156000 12.524889 13.709333
#> 1995 15.340444 16.870556 18.403889 21.477593 20.155556 22.772963 15.067556
#> 1996  5.457778  6.942778  7.945000  6.713333  7.267222  8.225556  9.797222
#> 1997 10.245000 14.838889 18.900556 13.018333 20.670556 18.008333 25.101111
#> 1998 14.938889 22.014556 27.530111 25.211667 49.455778 36.952222 51.062667
#> 1999 13.180000  9.900000 10.150000 15.153333 15.023333 27.073333 27.793333
#> 2000  6.723333  7.568889  8.151111  6.603333 11.191111       NaN 17.072222
#> 2001 12.292778 10.526667  8.988889  7.461667  6.169444  6.562222  5.967222
#> 2002        NA        NA        NA        NA        NA        NA        NA
#> 2003 25.201111 25.397778 29.135000 33.476667 36.393426 39.528889 43.765556
#> 2004  9.077778  9.192222 11.063333 11.536667 11.284444 11.726667 10.812593
#> 2005  6.286667  6.453333  7.633333  7.463333  7.810833  8.453333  8.766667
#> 2006 20.582222 22.477778 26.878889 26.354444 29.867778 31.061111 33.715556
#> 2007 10.234444 10.537778 11.924444 12.008889 12.078889 13.150000 12.951667
#> 2008 16.969444 16.746667 20.165556 21.036667 21.838889 22.320000 23.547222
#>            s35       s36
#> 1978       NaN       NaN
#> 1979  7.933333  7.200000
#> 1980 14.050000 12.240000
#> 1981       NaN       NaN
#> 1982       NaN       NaN
#> 1983       NaN       NaN
#> 1984       NaN       NaN
#> 1985       NaN       NaN
#> 1986       NaN       NaN
#> 1987       NaN       NaN
#> 1988       NaN       NaN
#> 1989       NaN       NaN
#> 1990       NaN       NaN
#> 1991       NaN       NaN
#> 1992  4.739333  4.864000
#> 1993 16.885556 17.823889
#> 1994 14.444889 13.420889
#> 1995 15.329444 13.493148
#> 1996  9.785556 10.080000
#> 1997 22.151111 28.362778
#> 1998 48.988333 56.544444
#> 1999 23.393333       NaN
#> 2000       NaN 19.282778
#> 2001  5.926111  6.015556
#> 2002        NA        NA
#> 2003 41.094444 45.056296
#> 2004 10.232222  9.681111
#> 2005  8.030000  8.410000
#> 2006 32.142222 33.153333
#> 2007 13.016667 15.060000
#> 2008 23.382222 24.001111

monthNum(as.Date(c("2007-03-17", "2003-06-01")))
#> [1] 3 6

## Ignore certain seasons in a Seasonal Kendall test.
c27 <- sfbayChla[, "s27"]
seaKen(tsSub(c27))  # Aug and Dec missing the most key data
#> $sen.slope
#> [1] 0.1083333
#> 
#> $sen.slope.rel
#> [1] 0.04893086
#> 
#> $p.value
#> [1] 1.117981e-25
#> 
#> $miss
#> [1] 0.083
#> 
seaKen(tsSub(c27, seas = c(1:7, 9:11)))
#> $sen.slope
#> [1] 0.1083333
#> 
#> $sen.slope.rel
#> [1] 0.04707233
#> 
#> $p.value
#> [1] 1.903788e-21
#> 
#> $miss
#> [1] 0
#> 

y <- Sys.time()
years(y)
#> [1] 2024