The idx()
method converts indices.
The type of output depends on the type of input index arguments given:
idx(x, i = i, ...)
converts linear indices to a strictly positive integer vector of linear indices.idx(x, sub = sub, dims = dims, ...)
converts dimensional indices to a strictly positive integer vector of linear indices.idx(x, slice = slice, margin = margin, ...)
converts indices of one dimension to a strictly positive integer vector of indices for that specific dimension.
Vectors (both atomic and recursive) only have index argument i
.
Data.frame-like objects only have the slice, margin
index argument pair.
Arrays (both atomic and recursive) have the sub, dims
index argument pair,
as well as the arguments i
and slice, margin
.
The result of the idx()
method
can be used inside the regular square-brackets operators.
For example like so:
x <- array(...)
my_sub2ind <- idx(x, sub, dims)
x[my_sub2ind] <- value
y <- data.frame(...)
rows <- idx(y, 1:10, 1, inv = TRUE)
cols <- idx(y, c("a", "b"), 2)
y[rows, cols] <- value
thus allowing the user to benefit from the convenient index translations from 'squarebrackets',
whilst still using R's default copy-on-modification semantics
(instead of the semantics provided by 'squarebrackets').
Usage
idx(x, ...)
# S3 method for default
idx(x, i, inv = FALSE, ..., chkdup = getOption("squarebrackets.chkdup", FALSE))
# S3 method for array
idx(
x,
sub = NULL,
dims = 1:ndims(x),
slice = NULL,
margin = NULL,
i = NULL,
inv = FALSE,
...,
chkdup = getOption("squarebrackets.chkdup", FALSE)
)
# S3 method for data.frame
idx(
x,
slice,
margin,
inv = FALSE,
...,
chkdup = getOption("squarebrackets.chkdup", FALSE)
)
Arguments
- x
vector, matrix, array, or data.frame; both atomic and recursive objects are supported.
- ...
- i, sub, dims, margin, slice, inv
See squarebrackets_indx_args.
Duplicates are not allowed.- chkdup
Value
For idx(x, i = i, ...)
and idx(x, sub = sub, dims = dims, ...)
:
A strictly positive integer vector of flat indices.
For idx(x, margin = margin, slice = slice, ...)
:
A strictly positive integer vector of indices
for the dimension specified in margin
.
Examples
# atomic ====
x <- 1:10
x[idx(x, \(x)x>5)] <- -5
print(x)
#> [1] 1 2 3 4 5 -5 -5 -5 -5 -5
x <- array(1:27, dim = c(3,3,3))
x[idx(x, n(1:2, 1:2), c(1,3))] <- -10
print(x)
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] -10 -10 -10
#> [2,] -10 -10 -10
#> [3,] 3 6 9
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] -10 -10 -10
#> [2,] -10 -10 -10
#> [3,] 12 15 18
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 19 22 25
#> [2,] 20 23 26
#> [3,] 21 24 27
#>
################################################################################
# recursive ====
x <- as.list(1:10)
x[idx(x, \(x)x>5)] <- -5
print(x)
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 2
#>
#> [[3]]
#> [1] 3
#>
#> [[4]]
#> [1] 4
#>
#> [[5]]
#> [1] 5
#>
#> [[6]]
#> [1] -5
#>
#> [[7]]
#> [1] -5
#>
#> [[8]]
#> [1] -5
#>
#> [[9]]
#> [1] -5
#>
#> [[10]]
#> [1] -5
#>
x <- array(as.list(1:27), dim = c(3,3,3))
x[idx(x, n(1:2, 1:2), c(1,3))] <- -10
print(x)
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] -10 -10 -10
#> [2,] -10 -10 -10
#> [3,] 3 6 9
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] -10 -10 -10
#> [2,] -10 -10 -10
#> [3,] 12 15 18
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 19 22 25
#> [2,] 20 23 26
#> [3,] 21 24 27
#>
x <- data.frame(
a = sample(c(TRUE, FALSE, NA), 10, TRUE),
b = 1:10,
c = rnorm(10),
d = letters[1:10],
e = factor(letters[11:20])
)
rows <- idx(x, 1:5, 1, inv = TRUE)
cols <- idx(x, c("b", "a"), 2)
x[rows, cols] <- NA
print(x)
#> a b c d e
#> 1 FALSE 1 0.3720032 a k
#> 2 FALSE 2 -0.6098583 b l
#> 3 FALSE 3 1.0439389 c m
#> 4 TRUE 4 -0.3027063 d n
#> 5 TRUE 5 1.4172813 e o
#> 6 NA NA -0.8671011 f p
#> 7 NA NA -2.2181411 g q
#> 8 NA NA -0.2753020 h r
#> 9 NA NA 0.3766451 i s
#> 10 NA NA 0.9721363 j t