Skip to contents

This function takes a data frame and a vector of column names as argument and returns a logical value indicating whether the indicated columns uniquely identify entries in the data frame. If the output is TRUE, the indicated columns could be the keys of the table.

Usage

is_keycol(x, cols, allow_NA = FALSE, verbose = TRUE)

Arguments

x

A data frame object

cols

A vector of column names or indices to be tested.

allow_NA

Logical value indicating whether to allow key columns to have NA values. Default is allow_NA=FALSE, the function will return FALSE if any NA value is present in colnames.

verbose

Logical value indicating whether messages should be printed on the console. Default is TRUE.

Value

Returns a logical value. If TRUE, the columns indicated in colnames uniquely identify the entries in x.

Examples

is_keycol(data.frame(a=1:10,b=sample(c("a","b","c"),10, replace=TRUE)), cols="a")
#> [1] TRUE
is_keycol(data.frame(a=1:10,b=sample(c("a","b","c"),10, replace=TRUE)), cols="b")
#> [1] FALSE
is_keycol(
data.frame(a=c(1:5,1:5),
b=sample(c("a","b","c"),10, replace=TRUE),
c=c(rep("a",5),rep("b",5))),
cols=c("a","c"))
#> [1] TRUE