Description Usage Arguments Details Value See Also Examples
extract is a function that converts different index types such as
negative integer vectors, character vectors, or logical vectors passed to
the [ function as i (e.g. X[i]) or i and
j (e.g. X[i, j]) into positive integer vectors. The
converted indices are provided as the i parameter of
extract_vector or i and j parameters of
extract_matrix to facilitate implementing the extraction mechanism
for custom matrix-like types.
1 |
extract_vector |
A function in the form of |
extract_matrix |
A function in the form of |
allowDoubles |
If set, indices of type double are not converted to integers if the
operation would overflow to support matrices with |
The custom type must implement methods for length,
dim and dimnames for this function
to work. Implementing methods for nrow,
ncol, rownames, and
colnames is not necessary as the default method of
those generics calls dim or
dimnames internally.
Optional arguments are supported and will be passed to
extract_vector and extract_matrix as long as they are named.
A function in the form of function(x, i, j, ..., drop = TRUE) that
is meant to be used as a method for [ for a custom
type.
vignette("StringMatrix", package = "crochet") for a vignette
containing a complete example on how to use extract to implement
[ for a custom type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | b <- matrix(data = rnorm(25), nrow = 5, ncol = 5)
dimnames(b) <- list(letters[1:5], letters[1:5])
a <- structure(list(), class = "TestMatrix")
dim.TestMatrix <- function(x) {
dim(b)
}
dimnames.TestMatrix <- function(x) {
dimnames(b)
}
extract_vector <- function(x, i) {
# Dispatch to b instead to x for this demo
b[i, drop = FALSE]
}
extract_matrix <- function(x, i, j) {
# Dispatch to b instead to x for this demo
b[i, j, drop = FALSE]
}
`[.TestMatrix` <- extract(extract_vector = extract_vector, extract_matrix = extract_matrix)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.