partition {unpivotr} | R Documentation |
Divide a grid of cells into partitions containing individual tables
Description
Given the positions of corner cells that mark individual tables in a single
spreadsheet, partion()
works out which table cells belong to which corner
cells. The individual tables can then be worked on independently.
partition()
partitions along both dimensions (rows and columns) at once.
partition_dim()
partitions along one dimension at a time.
Usage
partition(cells, corners, align = "top_left", nest = TRUE, strict = TRUE)
partition_dim(positions, cutpoints, bound = "upper")
Arguments
cells |
Data frame or tbl, the cells to be partitioned, from
|
corners |
usually a subset of |
align |
Character, the position of the corner cells relative to their
tables, one of |
nest |
Logical, whether to nest the partitions in a list-column of data frames. |
strict |
Logical, whether to omit partitions that don't contain a corner cell. |
positions |
Integer vector, the positions of cells (either the row position or the column position), which are to be grouped between cutpoints. |
cutpoints |
Integer vector. The |
bound |
One of |
Value
partition_dim()
returns an integer vector, numbering the groups of
cells. Group 0 represents the cells above the first cutpoint (when bound = "upper"
), or below the first cutpoint (when bound = "lower"
). The
other groups are numbered from 1, where group 1 is adjacent to group 0.
partition_dim()
returns an integer vector, numbering the groups of cells.
Group 0 represents the cells above the first cutpoint (when bound = "upper"
), or below the first cutpoint (when bound = "lower"
). The other
groups are numbered from 1, where group 1 is adjacent to group 0. Divide a
grid of cells into chunks along both dimensions
Functions
-
partition_dim()
: Divide a grid of cells into chunks along one dimension
Examples
# The `purpose` dataset, represented in four summary tables
multiples <- purpose$small_multiples
rectify(multiples, character, numeric)
# The same thing in its raw 'melted' form that can be filtered
multiples
# First, find the cells that mark a corner of each table
corners <-
dplyr::filter(multiples,
!is.na(character),
!(character %in% c("Sex", "Value", "Female", "Male")))
# Then find out which cells fall into which partition
partition(multiples, corners)
# You can also use bottom-left corners (or top-right or bottom-right)
bl_corners <- dplyr::filter(multiples, character == "Male")
partition(multiples, bl_corners, align = "bottom_left")
# To complete the grid even when not all corners are supplied, use `strict`
bl_corners <- bl_corners[-1, ]
partition(multiples, bl_corners, align = "bottom_left")
partition(multiples, bl_corners, align = "bottom_left", strict = FALSE)
# Given a set of cells in rows 1 to 10, partition them at the 3rd, 5th and 7th
# rows.
partition_dim(1:10, c(3, 5, 7))
# Given a set of cells in columns 1 to 10, partition them at the 3rd, 5th and
# 7th column. This example is exactly the same as the previous one, to show
# that the function works the same way on columns as rows.
partition_dim(1:10, c(3, 5, 7))
# Given a set of cells in rows 1 to 10, partition them at the 3rd, 5th and
# 7th rows, aligned to the bottom of the group.
partition_dim(1:10, c(3, 5, 7), bound = "lower")
# Non-integer row/column numbers and cutpoints can be used, even though they
# make no sense in the context of partioning grids of cells. They are
# rounded towards zero first.
partition_dim(1:10 - .5, c(3, 5, 7))
partition_dim(1:10, c(3, 5, 7) + 1.5)