An alias for tidytext::cast_sparse
Arguments
- data
Dataframe
- row
Column name to use as row names, as string or symbol
- column
Column name to use as column names, as string or symbol
- value
Column name to use as matrix values, as string or symbol
- ...
Other arguments, passed to
Matrix::sparseMatrix
- sparse
Should the matrix be a
Matrix
sparse matrix?
Value
A matrix or sparse Matrix object, with one row for each unique value in the row column, one column for each unique value in the column column, and with as many non-zero values as there are rows in data.
Examples
data.frame(id = c(1, 1, 2, 2) + 4,
cols = c('a', 'b', 'a', 'b'),
vals = 1:4) |>
build_matrix(row = id, column = 'cols', value = vals)
#> 2 x 2 sparse Matrix of class "dgCMatrix"
#> a b
#> 5 1 2
#> 6 3 4