R/library_data_structures.R

Defines functions df_xtab_matrix

Documented in df_xtab_matrix

#' Cross-tabulates columns of a dataframe into a matrix with NAs for unspecified pairs
#' @param model A \code{formula} indicating relevant columns from data frame
#' @param df A \code{data.frame} of columns to cross-tabulate
#' @param rows A \code{vector} of row names for the matrix to sort by
#' @param columns A \code{vector} of column names for the matrix to sort by
#' @return A cross-tabulated matrix matrix with NAs for unspecified pairs.
df_xtab_matrix <- function(model, df, rows, columns) {
  # identify required relationships
  model_parts <- strsplit(deparse(model), '~')[[1]]
  ones <- rep(1,nrow(df))
  params_model <- stats::as.formula(paste("ones", '~', model_parts[2]))
  params <- stats::xtabs(params_model, df)

  # create model matrix
  stats::xtabs(model, df) -> .
  .[params == 0] <- NA
  as.data.frame.matrix(.) -> .
  as.matrix(.[rows, columns])
}

Try the seminr package in your browser

Any scripts or data that you put into this service are public.

seminr documentation built on Oct. 13, 2022, 1:05 a.m.