Details on Casting Functions

Description

‘broadcast’ provides several "casting" functions.
These can facility complex forms of broadcasting that would normally not be possible.
But these "casting" functions also have their own merit, beside empowering complex broadcasting.

The following casting functions are available:

  • acast:
    casts group-based subsets of an array into a new dimension.
    Useful for, for example, performing grouped broadcasted operations.

  • cast_hier2dim:
    casts a nested/hierarchical list into a dimensional list (i.e. array of type list).
    Useful because one cannot broadcast through nesting, but one can broadcast along dimensions.

  • cast_dim2hier:
    casts a dimensional list into a nested/hierarchical list; the opposite of cast_hier2dim.

  • cast_dim2flat:
    casts a dimensional list into a flattened list, but with names that indicate their original dimensional positions.
    Mostly useful for printing or summarizing dimensional lists.

  • dropnests:
    drop redundant nesting in lists; mostly used for facilitating the above casting functions.

Shared Argument in2out

The hier2dim, cast_hier2dim, and cast_dim2hier methods all have the in2out argument.


in2out = TRUE
By default in2out is TRUE.
This means the call
y <- cast_hier2dim(x)
will cast the elements of the deepest valid depth of x to the rows of y, and elements of the depth above that to the columns of y, and so on until the surface-level elements of x are cast to the last dimension of y.

Similarly, the call
x <- cast_dim2hier(y)
will cast the rows of y to the inner most elements of x, and the columns of y to one depth above that, and so on until the last dimension of y is cast to the surface-level elements of x.

Consider the nested list x with a depth of 3, and the recursive array y with 3 dimensions, where their relationship can described as the following code:
y <- cast_hier2dim(x)
x <- cast_dim2hier(y).
Then it holds that:
x[[i]][[j]][[k]] corresponds to y[[k, j, i]],
\(\forall\)(i, j, k) , provided x[[i]][[j]][[k]] exists.


in2out = FALSE
If in2out = FALSE, the call
y <- cast_hier2dim(x, in2out = FALSE)
will cast the surface-level elements of x to the rows of y, and elements of the depth below that to the columns of y, and so on until the elements of the deepest valid depth of x are cast to the last dimension of y.

Similarly, the call
x <- cast_dim2hier(y, in2out = FALSE)
will cast the rows of y to the surface-level elements of x, and the columns of y to one depth below that, and so on until the last dimension of y is cast to the inner most elements of x.

Consider the nested list x with a depth of 3, and the recursive array y with 3 dimensions, where their relationship can described with the following code:
y <- cast_hier2dim(x, in2out = FALSE)
x <- cast_dim2hier(y, in2out = FALSE).
Then it holds that :
x[[i]][[j]][[k]] corresponds to y[[i, j, k]],
\(\forall\)(i, j, k) , provided x[[i]][[j]][[k]] exists.