Learn R Programming

crochet (version 2.3.0)

convertIndex: Convert Non-Numeric Index Types to Positive Integers

Description

Converts different index types such as negative integer vectors, character vectors, or logical vectors into positive integer vectors.

Usage

convertIndex(x, i, type, allowDoubles = FALSE)

Arguments

x

A matrix-like object.

i

The index to convert: may be a one-dimensional or two-dimensional logical, character, integer, or double vector.

type

The type of index to convert to: k is a one-dimensional index, i is the part of a two-dimensional index that determines the rows, and j is the part of a two-dimensional index that determines the columns.

allowDoubles

If set, indices of type double are not converted to integers if the operation would overflow to support matrices with nrow(), ncol(), or length() greater than the largest integer that can be represented (.Machine$integer.max).

Value

The converted index.

See Also

extract and replace to generate implementations for [ and [<- for custom types that use convertIndex under the hood.

Examples

Run this code
# NOT RUN {
x <- matrix(data = rnorm(25), nrow = 5, ncol = 5)
dimnames(x) <- list(letters[1:5], letters[1:5])

convertIndex(x, c(1, 2, 3), "k")
convertIndex(x, -25, "k")
convertIndex(x, c(TRUE, FALSE), "k")

convertIndex(x, c(1, 2, 3), "i")
convertIndex(x, -5, "i")
convertIndex(x, c(TRUE, FALSE), "i")
convertIndex(x, c("a", "b", "c"), "i")

convertIndex(x, c(1, 2, 3), "j")
convertIndex(x, -5, "j")
convertIndex(x, c(TRUE, FALSE), "j")
convertIndex(x, c("a", "b", "c"), "j")
# }

Run the code above in your browser using DataLab