subset.tidytensor.Rd
A functional form of e.g. tensor[1:10, 3, ]
, supporting selecting by ranknames, usage with
indexing when the rank is unknown.
# S3 method for tidytensor
subset(x, ..., drop = TRUE)
the tidytensor to apply over.
named or unnamed parameters specifying subsetting criteria (see examples)
whether to drop ranks with size 1 (similar to x[..., drop = TRUE]
)
a tidytensor
Subsetting a tidytensor with subset()
as opposed to []
allows for subsetting even when the number of ranks of the input is unknown; see examples.
# shape [100, 26, 26, 3]
t <- as.tidytensor(array(rnorm(100 * 26 * 26 * 3), dim = c(100, 26, 26, 3)))
ranknames(t) <- c("sample", "row", "col", "channel")
t <- set_dimnames_for_rank(t, channel, R, G, B)
print(t)
#> # Rank 4 tensor, shape: (100, 26, 26, 3), ranknames: sample, row, col, channel
#> | # Rank 3 tensor, shape: (26, 26, 3)
#> | [-1.04, -0.518, 0.275] [0.753, 1.18, -0.67] [-0.934, 0.536, -0.647] [-0.73, -0.394, 1.03] [-0.527, -0.605, -0.704] [2.68, -0.184, -1.83] ...
#> | [-0.23, 0.629, 0.6] [-0.394, 0.106, -0.155] [-0.147, -0.934, -0.724] [1.99, 0.394, -0.0729] [-1.51, 0.248, 1.91] [0.0827, 0.0644, 2.42] ...
#> | [0.574, -0.68, -0.288] [0.151, 0.0998, 0.824] [1.12, -1.82, 1.83] [-1.19, 0.119, -1.45] [-0.787, -1.35, 1.16] [-0.509, -0.383, -0.345] ...
#> | [1.63, 0.187, -1.34] [-0.268, 1.16, -1.5] [0.639, 1.92, -1.11] [1.25, 0.643, 0.311] [0.217, 0.772, -0.989] [1.66, 1.41, 0.531] ...
#> | [-0.00906, -0.923, 0.281] [0.347, 1.02, 1.75] [0.0826, -0.18, -1.82] [0.149, 0.0708, -0.663] [0.709, 0.924, 0.336] [0.205, -1.15, -0.0916] ...
#> | [-0.671, 1.51, 0.512] [0.911, 0.154, -1.07] [-0.465, 0.0816, 0.635] [0.173, 0.776, -0.583] [-0.646, 1.37, -0.0359] [-1.2, 0.526, 0.794] ...
#> | ... ... ... ... ... ... ...
#> | # ...
t2 <- subset(t, row = 1:10, sample = 27, drop = FALSE)
print(t2)
#> # Rank 4 tensor, shape: (1, 10, 26, 3), ranknames: sample, row, col, channel
#> | # Rank 3 tensor, shape: (10, 26, 3)
#> | [-1.13, 0.44, 0.392] [2.31, 1.26, 0.455] [0.439, -1.69, 0.617] [-0.875, 2.22, 0.622] [0.677, -0.621, -2.44] [0.0493, 1.42, 2.06] ...
#> | [0.404, 0.295, -0.342] [-0.871, 0.318, -1.44] [0.00557, -0.664, 0.605] [1.97, -1.1, 0.198] [-2.21, 0.118, 2.02] [-0.914, 1.57, -1.37] ...
#> | [-0.166, -0.935, 0.757] [-0.582, -0.546, 0.267] [-0.642, 0.307, 2.22] [1.68, 1.53, -0.288] [-0.914, 1.62, -1.34] [0.195, 0.762, 1.47] ...
#> | [0.442, 0.368, -0.202] [-0.092, -0.899, -0.4] [0.47, 0.0738, 1.29] [1.91, -0.699, 2] [0.932, -0.337, 0.331] [-2.22, 1.16, -1.07] ...
#> | [0.899, -0.297, 0.558] [-0.0197, -1.24, 0.4] [-0.115, -0.769, -0.369] [-1.96, 0.35, -0.0796] [0.687, -1.17, -1.1] [-0.797, -0.336, 1.55] ...
#> | [-0.434, -0.643, 0.681] [-0.104, -0.518, 0.792] [-0.205, -1.67, 0.403] [-0.476, -1.66, 0.385] [2.03, -0.673, -1.87] [0.315, 0.827, 0.207] ...
#> | ... ... ... ... ... ... ...
#> |
# same thing, but without named ranks (not a good idea to mixes named
# subsetting and non-named subsetting)
t2 <- subset(t, 27, 1:10, drop = FALSE)
print(t2)
#> # Rank 4 tensor, shape: (1, 10, 26, 3), ranknames: sample, row, col, channel
#> | # Rank 3 tensor, shape: (10, 26, 3)
#> | [-1.13, 0.44, 0.392] [2.31, 1.26, 0.455] [0.439, -1.69, 0.617] [-0.875, 2.22, 0.622] [0.677, -0.621, -2.44] [0.0493, 1.42, 2.06] ...
#> | [0.404, 0.295, -0.342] [-0.871, 0.318, -1.44] [0.00557, -0.664, 0.605] [1.97, -1.1, 0.198] [-2.21, 0.118, 2.02] [-0.914, 1.57, -1.37] ...
#> | [-0.166, -0.935, 0.757] [-0.582, -0.546, 0.267] [-0.642, 0.307, 2.22] [1.68, 1.53, -0.288] [-0.914, 1.62, -1.34] [0.195, 0.762, 1.47] ...
#> | [0.442, 0.368, -0.202] [-0.092, -0.899, -0.4] [0.47, 0.0738, 1.29] [1.91, -0.699, 2] [0.932, -0.337, 0.331] [-2.22, 1.16, -1.07] ...
#> | [0.899, -0.297, 0.558] [-0.0197, -1.24, 0.4] [-0.115, -0.769, -0.369] [-1.96, 0.35, -0.0796] [0.687, -1.17, -1.1] [-0.797, -0.336, 1.55] ...
#> | [-0.434, -0.643, 0.681] [-0.104, -0.518, 0.792] [-0.205, -1.67, 0.403] [-0.476, -1.66, 0.385] [2.03, -0.673, -1.87] [0.315, 0.827, 0.207] ...
#> | ... ... ... ... ... ... ...
#> |
# equiv of t3[1:20, , , c("G", "R", "B")] # since the last rank has dimnames()
# note the re-ordering of channel levels
t3 <- subset(t, sample = 1:20, channel = c("G", "R", "B"), drop = FALSE)
print(t3)
#> # Rank 4 tensor, shape: (20, 26, 26, 3), ranknames: sample, row, col, channel
#> | # Rank 3 tensor, shape: (26, 26, 3)
#> | [-0.518, -1.04, 0.275] [1.18, 0.753, -0.67] [0.536, -0.934, -0.647] [-0.394, -0.73, 1.03] [-0.605, -0.527, -0.704] [-0.184, 2.68, -1.83] ...
#> | [0.629, -0.23, 0.6] [0.106, -0.394, -0.155] [-0.934, -0.147, -0.724] [0.394, 1.99, -0.0729] [0.248, -1.51, 1.91] [0.0644, 0.0827, 2.42] ...
#> | [-0.68, 0.574, -0.288] [0.0998, 0.151, 0.824] [-1.82, 1.12, 1.83] [0.119, -1.19, -1.45] [-1.35, -0.787, 1.16] [-0.383, -0.509, -0.345] ...
#> | [0.187, 1.63, -1.34] [1.16, -0.268, -1.5] [1.92, 0.639, -1.11] [0.643, 1.25, 0.311] [0.772, 0.217, -0.989] [1.41, 1.66, 0.531] ...
#> | [-0.923, -0.00906, 0.281] [1.02, 0.347, 1.75] [-0.18, 0.0826, -1.82] [0.0708, 0.149, -0.663] [0.924, 0.709, 0.336] [-1.15, 0.205, -0.0916] ...
#> | [1.51, -0.671, 0.512] [0.154, 0.911, -1.07] [0.0816, -0.465, 0.635] [0.776, 0.173, -0.583] [1.37, -0.646, -0.0359] [0.526, -1.2, 0.794] ...
#> | ... ... ... ... ... ... ...
#> | # ...