keys-set {keyholder} | R Documentation |
Set keys
Description
Key is a vector which goal is to provide information about rows in reference
data frame. Its length should always be equal to number of rows in
data frame. Keys are stored as tibble in attribute "keys"
and so one data frame can have multiple keys. Data frame with keys is
implemented as class keyed_df.
Usage
keys(.tbl) <- value
assign_keys(.tbl, value)
key_by(.tbl, ..., .add = FALSE, .exclude = FALSE)
unkey(.tbl)
Arguments
.tbl |
Reference data frame . |
value |
Values of keys (converted to tibble). |
... |
Variables to be used as keys defined in similar fashion as in
|
.add |
Whether to add keys to (possibly) existing ones. If |
.exclude |
Whether to exclude key variables from |
Details
key_by
ignores grouping when creating keys. Also if .add == TRUE
and names of some added keys match the names of existing keys the new ones
will override the old ones.
Value for keys<-
should not be NULL
because it is converted to tibble
with zero rows. To remove keys use unkey()
, remove_keys()
or
restore_keys()
. assign_keys
is a more suitable for piping wrapper for
keys<-
.
See Also
Examples
df <- dplyr::as_tibble(mtcars)
# Value is converted to tibble
keys(df) <- 1:nrow(df)
# This will throw an error
## Not run:
keys(df) <- 1:10
## End(Not run)
# Use 'vs' and 'am' as keys
df %>% key_by(vs, am)
df %>% key_by(vs, am, .exclude = TRUE)
df %>% key_by(vs) %>% key_by(am, .add = TRUE, .exclude = TRUE)
# Override keys
df %>% key_by(vs, am) %>% dplyr::mutate(vs = 1) %>%
key_by(gear, vs, .add = TRUE)
# Use select helpers
df %>% key_by(dplyr::one_of(c("vs", "am")))
df %>% key_by(dplyr::everything())