Skip to contents

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)

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.

by

tidy-select. Columns in .data.

setkey

Logical. Should the output be keyed by by cols?

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.

Examples

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))
#>    x y n_vals
#> 1: a 2      2
#> 2: b 1      2

if (FALSE) {
assert_single_value(df, z, by = c(x, y))
}