Given a tidytensor, returns a data.frame, with each rank of the tensor being represented by a column. Produces an error if the resulting data.frame would have more than 10 million entries and allow_huge = FALSE.

# S3 method for tidytensor
as.data.frame(x, row.names = NULL, optional = FALSE, ...)

Arguments

x

input to convert to a data.frame

row.names

NULL (default) or character vector giving the new row names for the data frame (included for method compatibility with base as.data.frame).

optional

Ignored (included for method compatibility with base as.data.frame)

...

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

Value

a data.frame

Details

Note that this produces a row for each value in the tensor, and a column for each rank; data.frames are a much less efficient representation, but can be useful for e.g. visualization purposes. This method thus produces an error if the resulting data.frame would have more than 10 million entries and allow_huge = FALSE is set (default is TRUE). If dimnames() are set (naming each dimension withina rank), then the columns will be factors, rather than integer indices.

If the tidytensor ranks are not named, columns will be named index_1, index_2, etc., otherwise they will be set to ranknames. Tensor values will be in a column named value.

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")
df <- as.data.frame(t)
print(head(df))
#>   sample row pixel        value
#> 1      1   1     1 -1.400043517
#> 2      2   1     1  0.255317055
#> 3      3   1     1 -2.437263611
#> 4      4   1     1 -0.005571287
#> 5      5   1     1  0.621552721
#> 6      6   1     1  1.148411606

# Example with named dimensions:
dimnames(t)[[1]] <- paste("sample", 1:30, sep = "_")
dimnames(t)[[2]] <- paste("row", 1:26, sep = "_")
dimnames(t)[[3]] <- paste("pixel", 1:26, sep = "_")
# or with a list:
dimnames(t) <- list(paste("sample", 1:30, sep = "_"),
                    paste("row", 1:26, sep = "_"),
                    paste("pixel", 1:26, sep = "_"))

print(head(as.data.frame(t)))
#>     sample   row   pixel        value
#> 1 sample_1 row_1 pixel_1 -1.400043517
#> 2 sample_2 row_1 pixel_1  0.255317055
#> 3 sample_3 row_1 pixel_1 -2.437263611
#> 4 sample_4 row_1 pixel_1 -0.005571287
#> 5 sample_5 row_1 pixel_1  0.621552721
#> 6 sample_6 row_1 pixel_1  1.148411606