Skip to contents

The _icom() methods compute indices for copy-on-modify substitution.

x <- array(...)
my_ss2ii <- ss_icom(x, s, use)
x[my_ss2ii] <- value

y <- data.frame(...)
rows <- sbt_icom(y, 1:10, 1, inv = TRUE)
cols <- sbt_icom(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

ii_icom(x, i = NULL, use = 1, ...)

ss_icom(x, s = NULL, use = 1:ndim(x), ...)

sbt_icom(x, slice, use, ...)

# Default S3 method
ii_icom(
  x,
  i = NULL,
  use = 1,
  ...,
  chkdup = getOption("squarebrackets.chkdup", FALSE)
)

# Default S3 method
ss_icom(
  x,
  s = NULL,
  use = 1:ndim(x),
  ...,
  chkdup = getOption("squarebrackets.chkdup", FALSE)
)

# Default S3 method
sbt_icom(
  x,
  slice = NULL,
  use = NULL,
  ...,
  chkdup = getOption("squarebrackets.chkdup", FALSE)
)

Arguments

x

vector, matrix, array, or data.frame; both atomic and recursive objects are supported.

i, s, slice, use

See squarebrackets_indx_args.
Duplicates are not allowed.

...

see squarebrackets_method_dispatch.

chkdup

see squarebrackets_options.
[for performance: set to FALSE]

Value

A strictly positive numeric vector of indices.

Examples


# atomic ====

x <- 1:10
x[ii_icom(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[ss_icom(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[ii_icom(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[ss_icom(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 <- sbt_icom(x, 1:5, -1)
cols <- sbt_icom(x, c("b", "a"), 2)
x[rows, cols] <- NA
print(x)
#>        a  b          c d e
#> 1     NA  1 -0.1429676 a k
#> 2  FALSE  2  0.2893076 b l
#> 3     NA  3  0.3112620 c m
#> 4  FALSE  4 -0.6559543 d n
#> 5   TRUE  5  0.6253999 e o
#> 6     NA NA  0.2826962 f p
#> 7     NA NA  1.5976853 g q
#> 8     NA NA -1.1136539 h r
#> 9     NA NA -1.2694875 i s
#> 10    NA NA -0.3214222 j t