matrix_cols: Find columns that contain matrices

View source: R/utilities.R

matrix_colsR Documentation

Find columns that contain matrices


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.


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



The data frame to be queried for matrix columns.


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


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.


By default, a column is considered a matrix column if all() of the rows contain matrices. Use the .test_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.


A vector of integers saying which columns contain matrices.


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)) %>%
    rowtypes = "Industries",
    coltypes  = "Products"
  ) %>%
matsdf <- tidy %>%
  collapse_to_matrices(matnames = "matrix", matvals = "vals",
                       rownames = "row", colnames = "col",
                       rowtypes = "rowtypes", coltypes = "coltypes")
matrix_cols(matsdf, .drop_names = TRUE)

matsindf documentation built on Aug. 18, 2023, 5:06 p.m.