Learn R Programming

matsindf (version 0.4.11)

matrix_cols: Find columns that contain matrices

Description

It is often helpful to find the columns of a matsindf data frame that contain exclusively or some matrices. This function helps with that task.

Usage

matrix_cols(.df, .drop_names = FALSE, .any = FALSE)

Value

A vector of integers saying which columns contain matrices.

Arguments

.df

The data frame to be queried for matrix columns.

.drop_names

A boolean that tells whether to remove the names from the returned integer vector. Default is FALSE.

.any

A boolean that tells whether a column is reported when any() of the rows contain matrices (instead of all() rows contain matrices). Default is FALSE, in which case all entries in a column must be a matrix for the column to be reported.

Details

By default, a column is considered a matrix column if all() of the rows contain matrices. Use the .any argument to modify this behavior.

By default, the vector of integers returned from this function is named by the columns. Use the .drop_names function to modify this behavior.

Examples

Run this code
tidy <- tibble::tibble(matrix = c("V1", "V1", "V1", "V2", "V2"),
                         row = c("i1", "i1", "i2", "i1", "i2"),
                         col = c("p1", "p2", "p2", "p1", "p2"),
                         vals = c(1, 2, 3, 4, 5)) |>
  dplyr::mutate(
    rowtypes = "Industries",
    coltypes  = "Products"
  ) |>
  dplyr::group_by(matrix)
matsdf <- tidy |>
  collapse_to_matrices(matnames = "matrix", matvals = "vals",
                       rownames = "row", colnames = "col",
                       rowtypes = "rowtypes", coltypes = "coltypes")
matsdf
matrix_cols(matsdf)
matrix_cols(matsdf, .drop_names = TRUE)

Run the code above in your browser using DataLab