Take a subset along an arbitrary dimension

take(x, along, indices, drop = FALSE)

Arguments

x

matrix or array to subset

along

dimension to subset along

indices

the indices to select

drop

should the dimensions of the array be simplified? Defaults to FALSE which is the opposite of the useful R default.

Examples

x <- array(seq_len(3 * 4 * 5), c(3, 4, 5)) take(x, 3, 1)
#> , , 1 #> #> [,1] [,2] [,3] [,4] #> [1,] 1 4 7 10 #> [2,] 2 5 8 11 #> [3,] 3 6 9 12 #>
take(x, 2, 1)
#> , , 1 #> #> [,1] #> [1,] 1 #> [2,] 2 #> [3,] 3 #> #> , , 2 #> #> [,1] #> [1,] 13 #> [2,] 14 #> [3,] 15 #> #> , , 3 #> #> [,1] #> [1,] 25 #> [2,] 26 #> [3,] 27 #> #> , , 4 #> #> [,1] #> [1,] 37 #> [2,] 38 #> [3,] 39 #> #> , , 5 #> #> [,1] #> [1,] 49 #> [2,] 50 #> [3,] 51 #>
take(x, 1, 1)
#> , , 1 #> #> [,1] [,2] [,3] [,4] #> [1,] 1 4 7 10 #> #> , , 2 #> #> [,1] [,2] [,3] [,4] #> [1,] 13 16 19 22 #> #> , , 3 #> #> [,1] [,2] [,3] [,4] #> [1,] 25 28 31 34 #> #> , , 4 #> #> [,1] [,2] [,3] [,4] #> [1,] 37 40 43 46 #> #> , , 5 #> #> [,1] [,2] [,3] [,4] #> [1,] 49 52 55 58 #>
take(x, 3, 1, drop = TRUE)
#> [,1] [,2] [,3] [,4] #> [1,] 1 4 7 10 #> [2,] 2 5 8 11 #> [3,] 3 6 9 12
take(x, 2, 1, drop = TRUE)
#> [,1] [,2] [,3] [,4] [,5] #> [1,] 1 13 25 37 49 #> [2,] 2 14 26 38 50 #> [3,] 3 15 27 39 51
take(x, 1, 1, drop = TRUE)
#> [,1] [,2] [,3] [,4] [,5] #> [1,] 1 13 25 37 49 #> [2,] 4 16 28 40 52 #> [3,] 7 19 31 43 55 #> [4,] 10 22 34 46 58