Given a vector, matrix, or array, returns a tidytensor. If given a vector, converts to a 1-d array supporting dim(), matrices are left as matrices, and in all cases the class 'tidytensor' is added.

as.tidytensor(x, ...)

Arguments

x

input to convert to a tidytensor.

...

additional arguments to be passed to or from methods (ignored).

Value

a new tidytensor.

Details

Matrices are synonymous with 2-d arrays, so these are left as is. Vectors are converted to 1-d arrays so that they can support dim().

See also

Examples

# From an array (representing e.g. 30 26x26 images (30 sets of 26 rows of 26 pixels))
a <- array(rnorm(30 * 26 * 26), dim = c(30, 26, 26))
t <- as.tidytensor(a)
ranknames(t) <- c("sample", "row", "pixel")
print(t)
#> # Rank 3 tensor, shape: (30, 26, 26), ranknames: sample, row, pixel
#> |  # Rank 2 tensor, shape: (26, 26)
#> |        0.387  -0.873   -1.35  -0.592   0.322    2.43  ... 
#> |        -1.62   -1.58   0.663   -0.67   -1.05   0.474  ... 
#> |       -0.767   0.744  -0.182   -0.94  -0.966   0.409  ... 
#> |      -0.0849  -0.546  0.0324  -0.537    2.44    -1.8  ... 
#> |       -0.722  -0.881  -0.666  -0.245   0.403  -0.435  ... 
#> |       -0.425    1.58    1.31    0.42   0.115    1.61  ... 
#> |          ...     ...     ...     ...     ...     ...  ... 
#> |  # ...

# From a matrix (representing e.g. a 26x26 image (26 rows of 26 pixels))
m <- matrix(rnorm(26 * 26), nrow = 26, ncol = 26)
t <- as.tidytensor(m)
ranknames(t) <- c("row", "pixel")
print(t)
#> # Rank 2 tensor, shape: (26, 26), ranknames: row, pixel
#>     -0.932  -0.692   0.472   0.334     1.3  -0.253  ... 
#>      0.333   0.529  -0.596    -1.1   -1.15   0.574  ... 
#>     -0.794    1.22    1.73  -0.462   -1.22  -0.876  ... 
#>      0.608   -1.36   -1.21   0.843   -1.21    1.34  ... 
#>      0.396  -0.387  0.0262  -0.809  -0.715  -0.957  ... 
#>     -0.154  -0.921    1.38   -0.34   0.148   0.692  ... 
#>        ...     ...     ...     ...     ...     ...  ... 

# From a vector (representing e.g. 26 pixel values)
v <- rnorm(26)
t <- as.tidytensor(v)
ranknames(t) <- c("pixel")
print(t)
#> # Rank 1 tensor, shape: (26), ranknames: pixel
#>     -1.39  0.75  0.183  -1.76  0.532  -0.116  ...