subset.tidytensor: Subset dimensions of a tidytensor

Description Usage Arguments Details Value See Also Examples

View source: R/subset.R

Description

A functional form of e.g. tensor[1:10, 3, ], supporting selecting by ranknames, usage with indexing when the rank is unknown.

Usage

1
2
## S3 method for class 'tidytensor'
subset(x, ..., drop = TRUE)

Arguments

x

the tidytensor to apply over.

...

named or unnamed parameters specifying subsetting criteria (see examples)

drop

whether to drop ranks with size 1 (similar to x[..., drop = TRUE])

Details

Subsetting a tidytensor with subset() as opposed to [] allows for subsetting even when the number of ranks of the input is unknown; see examples.

Value

a tidytensor

See Also

shuffle, permute

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 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)

t2 <- subset(t, row = 1:10, sample = 27, drop = FALSE)
print(t2)

# 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)

# 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)

oneilsh/tidytensor documentation built on Oct. 11, 2021, 11:43 p.m.