Skip to contents

Methods to extract, exchange, or duplicate (i.e. repeat x times) subsets of an object.

Usage

i_x(x, ...)

i2_x(x, ...)

ss_x(x, ...)

ss2_x(x, ...)

# Default S3 method
i_x(x, i = NULL, ...)

# Default S3 method
ss_x(x, s = NULL, d = 1:ndim(x), ...)

# Default S3 method
i2_x(x, i = NULL, red = FALSE, ...)

# Default S3 method
ss2_x(x, s = NULL, d = 1:ndim(x), red = FALSE, ...)

# S3 method for class 'data.frame'
ss2_x(x, s = NULL, d = 1:2, obs = NULL, vars = NULL, ...)

Arguments

x

see squarebrackets_supported_structures.

...

see squarebrackets_method_dispatch.

i, s, d, obs, vars

See squarebrackets_indx_args.
Duplicates are allowed, resulting in duplicated indices.
An empty index selection results in an empty object of length 0.

red

Boolean, for recursive objects only, indicating if the result should be reduced.
If red = TRUE, selecting a single element will give the simplified result, like using [[]].
If red = FALSE, a list is always returned regardless of the number of elements.

Value

Returns a copy of the sub-setted object.

Examples



# atomic objects ====

obj <- matrix(1:16, ncol = 4)
colnames(obj) <- c("a", "b", "c", "a")
print(obj)
#>      a b  c  a
#> [1,] 1 5  9 13
#> [2,] 2 6 10 14
#> [3,] 3 7 11 15
#> [4,] 4 8 12 16
ss_x(obj, s = n(1:3), d = 1:ndim(obj))
#>      a b  c
#> [1,] 1 5  9
#> [2,] 2 6 10
#> [3,] 3 7 11
# above is equivalent to obj[1:3, 1:3, drop = FALSE]
i_x(obj, i = \(x) x > 5)
#>  [1]  6  7  8  9 10 11 12 13 14 15 16
# above is equivalent to obj[obj > 5]
ss_x(obj, s = n(c("a", "a")), d = 2L)
#>      a  a a  a
#> [1,] 1 13 1 13
#> [2,] 2 14 2 14
#> [3,] 3 15 3 15
#> [4,] 4 16 4 16
# above is equivalent to obj[, lapply(c("a", "a"), \(i) which(colnames(obj) == i)) |> unlist()]

obj <- array(1:64, c(4,4,3))
print(obj)
#> , , 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    5    9   13
#> [2,]    2    6   10   14
#> [3,]    3    7   11   15
#> [4,]    4    8   12   16
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   17   21   25   29
#> [2,]   18   22   26   30
#> [3,]   19   23   27   31
#> [4,]   20   24   28   32
#> 
#> , , 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   33   37   41   45
#> [2,]   34   38   42   46
#> [3,]   35   39   43   47
#> [4,]   36   40   44   48
#> 
ss_x(obj, s = n(1:3, 1:2), d = c(1,3))
#> , , 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    5    9   13
#> [2,]    2    6   10   14
#> [3,]    3    7   11   15
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   17   21   25   29
#> [2,]   18   22   26   30
#> [3,]   19   23   27   31
#> 
# above is equivalent to obj[1:3, , 1:2, drop = FALSE]
i_x(obj, i = \(x)x > 5)
#>  [1]  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
#> [26] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# above is equivalent to obj[obj > 5]


#############################################################################


# lists ====

obj <- list(a = 1:10, b = letters[1:11], c = 11:20)
print(obj)
#> $a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> $b
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k"
#> 
#> $c
#>  [1] 11 12 13 14 15 16 17 18 19 20
#> 
i2_x(obj, 1) # obj[1]
#> $a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
i2_x(obj, 1, red = TRUE) # obj[[1]]
#>  [1]  1  2  3  4  5  6  7  8  9 10
i2_x(obj, 1:2) # obj[1:2]
#> $a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> $b
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k"
#> 
i2_x(obj, is.numeric) # obj[sapply(obj, is.numeric)]
#> $a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> $c
#>  [1] 11 12 13 14 15 16 17 18 19 20
#> 
# for recursive subsets, see lst_rec()


obj <- rbind(
  lapply(1:4, \(x)sample(c(TRUE, FALSE, NA))),
  lapply(1:4, \(x)sample(1:10)),
  lapply(1:4, \(x)rnorm(10)),
  lapply(1:4, \(x)sample(letters))
)
colnames(obj) <- c("a", "b", "c", "a")
print(obj)
#>      a            b            c            a           
#> [1,] logical,3    logical,3    logical,3    logical,3   
#> [2,] integer,10   integer,10   integer,10   integer,10  
#> [3,] numeric,10   numeric,10   numeric,10   numeric,10  
#> [4,] character,26 character,26 character,26 character,26
ss2_x(obj, s = n(1:3), d = 1:ndim(obj))
#>      a          b          c         
#> [1,] logical,3  logical,3  logical,3 
#> [2,] integer,10 integer,10 integer,10
#> [3,] numeric,10 numeric,10 numeric,10
# above is equivalent to obj[1:3, 1:3, drop = FALSE]
i2_x(obj, i = is.numeric)
#> [[1]]
#>  [1]  7  9  2  8  5  6  1  3 10  4
#> 
#> [[2]]
#>  [1]  1.2035783 -0.6110229  0.2524835  0.4023584 -0.6803848  0.1499244
#>  [7] -0.3502410 -2.1339454  0.3784884 -1.0547823
#> 
#> [[3]]
#>  [1]  2  1  6  9  5 10  3  7  4  8
#> 
#> [[4]]
#>  [1] -1.7755111 -1.8710615  0.1210857  1.4267642  1.3059728 -0.5274928
#>  [7] -0.4127806 -0.5259307 -0.9535318  2.2325967
#> 
#> [[5]]
#>  [1]  3  1 10  4  2  5  8  7  9  6
#> 
#> [[6]]
#>  [1] -0.46899732  0.29847697  0.09520216  0.39083825  1.16925561  0.82209940
#>  [7] -0.12020603 -0.54341483 -0.24845973 -0.41923568
#> 
#> [[7]]
#>  [1]  6  4  5  9  1  7 10  2  3  8
#> 
#> [[8]]
#>  [1] -1.37862663 -0.23090823  0.89945718 -0.72264111  1.61219330  1.57576437
#>  [7]  0.39134382  1.69919267  1.31373830  0.06493015
#> 
# above is equivalent to obj[sapply(obj, is.numeric)]
ss2_x(obj, s = n(c("a", "a")), d = 2L)
#>      a            a            a            a           
#> [1,] logical,3    logical,3    logical,3    logical,3   
#> [2,] integer,10   integer,10   integer,10   integer,10  
#> [3,] numeric,10   numeric,10   numeric,10   numeric,10  
#> [4,] character,26 character,26 character,26 character,26
# above is equivalent to obj[, lapply(c("a", "a"), \(i) which(colnames(obj) == i)) |> unlist()]

obj <- array(as.list(1:64), c(4,4,3))
print(obj)
#> , , 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,] 1    5    9    13  
#> [2,] 2    6    10   14  
#> [3,] 3    7    11   15  
#> [4,] 4    8    12   16  
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,] 17   21   25   29  
#> [2,] 18   22   26   30  
#> [3,] 19   23   27   31  
#> [4,] 20   24   28   32  
#> 
#> , , 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,] 33   37   41   45  
#> [2,] 34   38   42   46  
#> [3,] 35   39   43   47  
#> [4,] 36   40   44   48  
#> 
ss2_x(obj, s = n(1:3, 1:2), d = c(1,3))
#> , , 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,] 1    5    9    13  
#> [2,] 2    6    10   14  
#> [3,] 3    7    11   15  
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,] 17   21   25   29  
#> [2,] 18   22   26   30  
#> [3,] 19   23   27   31  
#> 
# above is equivalent to obj[1:3, , 1:2, drop = FALSE]
i2_x(obj, i = \(x)x > 5)
#> [[1]]
#> [1] 6
#> 
#> [[2]]
#> [1] 7
#> 
#> [[3]]
#> [1] 8
#> 
#> [[4]]
#> [1] 9
#> 
#> [[5]]
#> [1] 10
#> 
#> [[6]]
#> [1] 11
#> 
#> [[7]]
#> [1] 12
#> 
#> [[8]]
#> [1] 13
#> 
#> [[9]]
#> [1] 14
#> 
#> [[10]]
#> [1] 15
#> 
#> [[11]]
#> [1] 16
#> 
#> [[12]]
#> [1] 17
#> 
#> [[13]]
#> [1] 18
#> 
#> [[14]]
#> [1] 19
#> 
#> [[15]]
#> [1] 20
#> 
#> [[16]]
#> [1] 21
#> 
#> [[17]]
#> [1] 22
#> 
#> [[18]]
#> [1] 23
#> 
#> [[19]]
#> [1] 24
#> 
#> [[20]]
#> [1] 25
#> 
#> [[21]]
#> [1] 26
#> 
#> [[22]]
#> [1] 27
#> 
#> [[23]]
#> [1] 28
#> 
#> [[24]]
#> [1] 29
#> 
#> [[25]]
#> [1] 30
#> 
#> [[26]]
#> [1] 31
#> 
#> [[27]]
#> [1] 32
#> 
#> [[28]]
#> [1] 33
#> 
#> [[29]]
#> [1] 34
#> 
#> [[30]]
#> [1] 35
#> 
#> [[31]]
#> [1] 36
#> 
#> [[32]]
#> [1] 37
#> 
#> [[33]]
#> [1] 38
#> 
#> [[34]]
#> [1] 39
#> 
#> [[35]]
#> [1] 40
#> 
#> [[36]]
#> [1] 41
#> 
#> [[37]]
#> [1] 42
#> 
#> [[38]]
#> [1] 43
#> 
#> [[39]]
#> [1] 44
#> 
#> [[40]]
#> [1] 45
#> 
#> [[41]]
#> [1] 46
#> 
#> [[42]]
#> [1] 47
#> 
#> [[43]]
#> [1] 48
#> 
# above is equivalent to obj[sapply(obj, \(x) x > 5)]

#############################################################################

# data.frame-like objects ====

obj <- data.frame(a = 1:10, b = letters[1:10], c = 11:20, d = factor(letters[1:10]))
print(obj)
#>     a b  c d
#> 1   1 a 11 a
#> 2   2 b 12 b
#> 3   3 c 13 c
#> 4   4 d 14 d
#> 5   5 e 15 e
#> 6   6 f 16 f
#> 7   7 g 17 g
#> 8   8 h 18 h
#> 9   9 i 19 i
#> 10 10 j 20 j
ss2_x(obj, n(1:3)) # obj[1:3, 1:3, drop = FALSE]
#>   a b  c
#> 1 1 a 11
#> 2 2 b 12
#> 3 3 c 13
ss2_x(obj, obs = ~ (a > 5) & (c < 19), vars = is.numeric)
#>   a  c
#> 1 6 16
#> 2 7 17
#> 3 8 18