Description Usage Arguments Details Value See Also Examples
replace 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 replace_vector or
i and j parameters of replace_matrix to facilitate
implementing the replacement mechanism for custom matrix-like types. Values
are recycled to match the replacement length.
1  | 
replace_vector | 
 A function in the form of   | 
replace_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.
A function in the form of function(x, i, j, ..., value) 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 replace 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45  | 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)
replace_vector <- function(x, i, value) {
    .GlobalEnv$i <- i
    .GlobalEnv$value <- value
    # Dispatch to b instead to x for this demo
    with(.GlobalEnv, b[i] <- value)
    # Don't forget to return x
    return(x)
}
replace_matrix <- function(x, i, j, value) {
    .GlobalEnv$i <- i
    .GlobalEnv$j <- j
    .GlobalEnv$value <- value
    # Dispatch to b instead to x for this demo
    with(.GlobalEnv, b[i, j] <- value)
    # Don't forget to return x
    return(x)
}
`[<-.TestMatrix` <- replace(replace_vector = replace_vector, replace_matrix = replace_matrix)
 | 
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.