count_values: Check for existence of multiple values per group
Description
count_values() returns values of by variables for which the .data has
multiple unique rows, along with the number of unique rows for each
combination of values, only considering columns in col.
assert_single_value() throws an error if there are multiple unique rows for
any combination of by variable values, only considering columns in col.
Usage
count_values(.data, col, by, setkey = FALSE)
assert_single_value(.data, col, by)
Value
count_values()
A data.table with the (filtered)
by columns and an additional column "n_vals" which shows the number of
unique rows in .data having the combination of by values shown
in the output row.
assert_single_value()
No return value. Called to throw an
error depending on the input.
Arguments
.data
A data frame or data table
col
tidy-select. Columns in .data. When counting the number of unique
rows, only the columns specified in col are considered.
df <- read.table(text = "
x y z
a 1 3
a 1 3
a 2 4
a 2 4
a 2 2
b 1 1
b 1 2
", header = TRUE)
count_values(df, z, by = c(x, y))
if (FALSE) {
assert_single_value(df, z, by = c(x, y))
}