as.data.frame.tidytensor: Convert a tidytensor to a data.frame representation.

Description Usage Arguments Details Value See Also Examples

View source: R/as.data.frame.tidytensor.R

Description

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.

Usage

1
2
## S3 method for class '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).

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.

Value

a data.frame

See Also

ranknames.

Examples

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

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

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