rray_flatten() squashes the dimensionality of x so that the result is a 1D object.

rray_flatten(x)

Arguments

x

A vector, matrix, array or rray.

Value

A 1D object with the same container type as x.

Details

This function is similar to as.vector(), but keeps the class of the object and can keep the dimension names if applicable.

Dimension names are kept using the same rules that would be applied if you would have called rray_reshape(x, prod(rray_dim(x))). Essentially this means that names in the first dimension are kept if x is either already a 1D vector, or a higher dimensional object with 1's in all dimensions except for the first one.

Examples

library(magrittr) x <- rray(1:10, c(5, 2)) rray_flatten(x)
#> <rray<int>[10]> #> [1] 1 2 3 4 5 6 7 8 9 10
# Dimension names are kept here # (2) -> (2) y <- rray(1:2) %>% rray_set_axis_names(1, letters[1:2]) rray_flatten(y)
#> <rray<int>[2]> #> a b #> 1 2
# And they are kept here # (2, 1) -> (2) y_one_col <- rray_reshape(y, c(2, 1)) rray_flatten(y_one_col)
#> <rray<int>[2]> #> a b #> 1 2
# But not here, since the size of the first dim changes # (1, 2) -> (2) y_one_row <- t(y_one_col) rray_flatten(y_one_row)
#> <rray<int>[2]> #> [1] 1 2