pivot_to_matrix {proporz} | R Documentation |
Pivot long data.frame to wide matrix and vice versa
Description
Create a matrix in 'wide' format from a data.frame with 3 columns with
pivot_to_matrix
or create a data.frame in long format from a matrix with
pivot_to_df.
Usage
pivot_to_matrix(df_long)
pivot_to_df(matrix_wide, value_colname = "values")
Arguments
df_long |
data.frame in long format with exactly 3 columns |
matrix_wide |
matrix in wide format |
value_colname |
name for the new value column in the resulting data.frame |
Details
These pivot functions are used to prepare data for biproporz()
in
pukelsheim()
. They are not supposed to cover general use cases or provide
customization. They mainly exist because reshape is hard to handle and the
package should have no dependencies.
Value
A data.frame with 3 columns or a matrix. Note that the results are sorted by the first and second column (data.frame) or row/column names (matrix).
Examples
# From data.frame to matrix
df = data.frame(party = c("A", "A", "A", "B", "B", "B"),
region = c("III", "II", "I", "I", "II", "III"),
seats = c(5L, 3L, 1L, 2L, 4L, 6L))
pivot_to_matrix(df)
# from matrix to data.frame
mtrx = matrix(1:6, nrow = 2)
pivot_to_df(mtrx)
# from matrix to data.frame using dimnames
dimnames(mtrx) <- list(party = c("A", "B"), region = c("I", "II", "III"))
pivot_to_df(mtrx, "seats")
# Note that pivot results are sorted
pivot_to_df(pivot_to_matrix(df)) == df[order(df[[1]], df[[2]]),]
[Package proporz version 1.5.0 Index]