This is an S3 Method to extract, exchange,
or duplicate (i.e. repeat x times) subsets of an object.
Use sb_x(x, ...)
if x
is an atomic object.
Use sb2_x(x, ...)
if x
is a recursive object (i.e. list or data.frame-like).
Usage
sb_x(x, ...)
# Default S3 method
sb_x(x, i = NULL, ...)
# S3 method for class 'array'
sb_x(x, s = NULL, d = 1:ndim(x), i = NULL, ...)
sb2_x(x, ...)
# Default S3 method
sb2_x(x, i = NULL, red = FALSE, ...)
# S3 method for class 'array'
sb2_x(x, s = NULL, d = 1:ndim(x), i = NULL, red = FALSE, ...)
# S3 method for class 'data.frame'
sb2_x(x, s = NULL, d = 1:2, obs = NULL, vars = NULL, ...)
Arguments
- x
- ...
- 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.
Ifred = TRUE
, selecting a single element will give the simplified result, like using[[]]
.
Ifred = FALSE
, a list is always returned regardless of the number of elements.
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
sb_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]
sb_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]
sb_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
#>
sb_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]
sb_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
#>
sb2_x(obj, 1) # obj[1]
#> $a
#> [1] 1 2 3 4 5 6 7 8 9 10
#>
sb2_x(obj, 1, red = TRUE) # obj[[1]]
#> [1] 1 2 3 4 5 6 7 8 9 10
sb2_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"
#>
sb2_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 sb2_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
sb2_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]
sb2_x(obj, i = is.numeric)
#> [[1]]
#> [1] 1 8 6 4 9 7 5 2 10 3
#>
#> [[2]]
#> [1] -0.6200828 1.1825995 -0.4723893 0.2634897 0.6651511 1.1936924
#> [7] 0.5158762 1.8326564 0.6770724 1.6481785
#>
#> [[3]]
#> [1] 6 7 10 5 2 8 1 4 3 9
#>
#> [[4]]
#> [1] 0.0547731 1.7430894 1.3813757 -0.1161016 -0.1092852 -0.9284028
#> [7] -0.8021348 -0.4919059 -1.2174959 -1.1273827
#>
#> [[5]]
#> [1] 4 6 5 10 1 7 9 8 2 3
#>
#> [[6]]
#> [1] 1.49425702 -0.94118807 -1.33232375 0.86319249 0.76131691 1.05497349
#> [7] 0.77182096 -0.94493549 2.76314838 -0.01232096
#>
#> [[7]]
#> [1] 9 1 7 4 10 8 2 3 5 6
#>
#> [[8]]
#> [1] 0.727052746 -0.517023924 -1.099039927 -0.375259643 -0.979732888
#> [6] -0.609942328 0.321499208 -0.982335993 1.551590735 0.001556281
#>
# above is equivalent to obj[sapply(obj, is.numeric)]
sb2_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
#>
sb2_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]
sb2_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
sb2_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
sb2_x(obj, obs = ~ (a > 5) & (c < 19), vars = is.numeric)
#> a c
#> 1 6 16
#> 2 7 17
#> 3 8 18