Build a matrix where the elements are values of a measure variable, and the rows and columns are formed by observed combinations of ID variables. The ID variables picked out by rows and cols must uniquely identify cells. to_matrix(), unlike stats::xtabs(), does not sum across multiple combinations of ID variables.
to_matrix(x, rows, cols, measure)
Arguments
x: A data frame.
rows: The ID variable(s) used to distinguish rows in the matrix.
cols: The ID variable(s) used to distinguish columns in the matrix.
measure: The measure variable, eg rates or counts.
Returns
A matrix
Examples
x <- expand.grid(age = c(0,1,2), sex = c("F","M"), region = c("A","B"), year =2000:2001)x$count <-1:24to_matrix(x, rows = c(age, sex), cols = c(region, year), measure = count)to_matrix(x, rows = c(age, sex, region), cols = year, measure = count)## cells not uniquely identifiedtry(to_matrix(x, rows = age, cols = sex, measure = count))