Description Usage Arguments See Also Examples
tdrply
is a functional programing utility to apply functions to specific
parts and variables of TDR datasets. It is based on a call to mapply
.
1 2 |
f |
function(s) to apply. The function has
to be written considering that it's first argument will be a subset of the TDR data
(columns according to |
cl |
character of numeric, the columns to select in the |
ty |
a kind of regular expression to subset diving periods of
Several symbols can be juxtaposed to bluid more complex groups e.g.
|
no |
the dive numbers to process. Keep default |
la |
list of additional arguments of |
no_match |
how to handle no match or partial match. See last example for details. |
obj |
a "ses" object.Optional if a default individual has been declared
with |
... |
additional arguments to be passed to |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | data(exses)
ind(exses)
## Apply function to each dive
tdrply(function(x) max(x$depth, na.rm = TRUE))
# or, using "cl" to subset columns and "..." to set "na.rm"
tdrply(max, "depth", na.rm = TRUE)
# other examples with "cl" and "..."
bsm <- tdrply(brokenstick, 1:2, npts = 10)
tdrply(plot, 1:2, no = 33) ; plot(bsm[["!_/#33"]], add = TRUE, enumerate = TRUE)
# use "no" to specify what dive numbers should be processed
(tmp <- tdrply(max, "depth", no = 111, na.rm = FALSE)) # dive no 111 only
tdrply(max, "depth", no = -111, na.rm = FALSE) # all dives but no 111
tdrply(max, "depth", no = exses$stat$max_depth == tmp, na.rm = FALSE) # logicals are accepeted
## How to use "ty": few examples
# apply "f" to each group delimited by &
tdrply(max, "light", ty = "!&_&/", no = 50:51)
# apply "f" previous surface or, if not found, next surface
tdrply(max, "light", ty = "-|~", no = 50:51)
# mix operators and use parentheses as desired
tdrply(max, "light", ty = "_!&_/&_(-|~)", no = 50:51)
# When "ty" syntax is not enought just give a data frame instead
df <- data.frame(start = seq(1, 5000, 1000),
end = seq(1001, 6000, 1000))
tdrply(max, "depth", df) # notice that names start with "cst" as "custom"
# An id for output names can be provided in third column
(df$id <- sample(10:20, 5, replace = FALSE))
tdrply(max, "depth", df)
## Provide different arguments to each group with "la" (List of Arguments)
opar <- par(no.readonly = TRUE); par(mfrow = c(2, 2))
tdrply(plot, 1:2, no = 50:53, la = list(col = 1:4))
par(opar)
## Not run:
## tdrply is vectorized over "f"
funs <- c(min_depth = min, max_depth = max)
tmp <- tdrply(funs, "depth", "_", no = 50:53, na.rm = TRUE)
as.data.frame(tmp)
## tdrply is vectorized over "ty" as well
tmp <- tdrply(funs, "depth", c("!", "/", surf = "-~"), no = 50:53, na.rm = TRUE)
as.data.frame(unlist(tmp, recursive = FALSE))
## Assuming that some dive bottom could not be properly defined
exses$delim[exses$delim$no_dive == 111, c("btt_st_idx", "btt_ed_idx")] <- c(NA, NA)
# choose how to handle missing periods using "no_match".
# a warning is printed anyway
tdrply(max, "depth", ty = "_", no = 110:112, no_match = "na") # default
tdrply(max, "depth", ty = "_", no = 110:112, no_match = "ignore")
tdrply(max, "depth", ty = "_", no = 110:112, no_match = "error")
# but not if the missing period can be ignored e.g:
tdrply(max, "depth", ty = "!_/", no = 110:112, no_match = "error")
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.