powered by
Attaches key metadata to a data frame, marking which column(s) form the unique identifier for rows. Keys are validated for uniqueness at creation.
key(.data, ..., .validate = TRUE, .strict = FALSE)key(.data) <- value
key(.data) <- value
A keyed data frame (class keyed_df).
keyed_df
A data frame or tibble.
Column names (unquoted) that form the key. Can be a single column or multiple columns for a composite key.
If TRUE (default), check that the key is unique.
TRUE
If TRUE, error on non-unique keys. If FALSE (default), warn but still attach the key.
FALSE
Character vector of column names to use as key.
df <- data.frame(id = 1:3, x = c("a", "b", "c")) key(df, id) # Composite key df2 <- data.frame(country = c("US", "US", "UK"), year = c(2020, 2021, 2020), val = 1:3) key(df2, country, year)
Run the code above in your browser using DataLab