Functions to manipulate keys.
remove_keys(.tbl, ..., .unkey = FALSE)
restore_keys(.tbl, ..., .remove = FALSE, .unkey = FALSE)
pull_key(.tbl, var)
rename_keys(.tbl, ...)
Reference data frame.
Variables to be used for operations defined in similar fashion as
in dplyr::select()
.
Whether to unkey()
.tbl
in case there are no keys left.
Whether to remove keys after restoring.
Parameter for dplyr::pull()
.
remove_keys()
removes keys defined with ...
.
restore_keys()
transfers keys defined with ...
into .tbl
and removes
them from keys
if .remove == TRUE
. If .tbl
is grouped the following
happens:
If restored keys don't contain grouping variables then groups don't change;
If restored keys contain grouping variables then result will be regrouped based on restored values. In other words restoring keys beats 'not-modifying' grouping variables rule. It is made according to the ideology of keys: they contain information about rows and by restoring you want it to be available.
pull_key()
extracts one specified column from keys with dplyr::pull()
.
rename_keys()
renames columns in keys using dplyr::rename()
.
df <- mtcars %>% dplyr::as_tibble() %>%
key_by(vs, am, .exclude = TRUE)
df %>% remove_keys(vs)
#> # A keyed object. Keys: am
#> # A tibble: 32 × 9
#> mpg cyl disp hp drat wt qsec gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 4 4
#> # … with 22 more rows
df %>% remove_keys(dplyr::everything())
#> # A keyed object. Keys: there are no keys.
#> # A tibble: 32 × 9
#> mpg cyl disp hp drat wt qsec gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 4 4
#> # … with 22 more rows
df %>% remove_keys(dplyr::everything(), .unkey = TRUE)
#> # A tibble: 32 × 9
#> mpg cyl disp hp drat wt qsec gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 4 4
#> # … with 22 more rows
df %>% restore_keys(vs)
#> # A keyed object. Keys: vs, am
#> # A tibble: 32 × 10
#> mpg cyl disp hp drat wt qsec gear carb vs
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 4 4 0
#> 2 21 6 160 110 3.9 2.88 17.0 4 4 0
#> 3 22.8 4 108 93 3.85 2.32 18.6 4 1 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 3 1 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 3 2 0
#> 6 18.1 6 225 105 2.76 3.46 20.2 3 1 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 3 4 0
#> 8 24.4 4 147. 62 3.69 3.19 20 4 2 1
#> 9 22.8 4 141. 95 3.92 3.15 22.9 4 2 1
#> 10 19.2 6 168. 123 3.92 3.44 18.3 4 4 1
#> # … with 22 more rows
df %>% restore_keys(vs, .remove = TRUE)
#> # A keyed object. Keys: am
#> # A tibble: 32 × 10
#> mpg cyl disp hp drat wt qsec gear carb vs
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 4 4 0
#> 2 21 6 160 110 3.9 2.88 17.0 4 4 0
#> 3 22.8 4 108 93 3.85 2.32 18.6 4 1 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 3 1 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 3 2 0
#> 6 18.1 6 225 105 2.76 3.46 20.2 3 1 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 3 4 0
#> 8 24.4 4 147. 62 3.69 3.19 20 4 2 1
#> 9 22.8 4 141. 95 3.92 3.15 22.9 4 2 1
#> 10 19.2 6 168. 123 3.92 3.44 18.3 4 4 1
#> # … with 22 more rows
df %>% restore_keys(dplyr::everything(), .remove = TRUE)
#> # A keyed object. Keys: there are no keys.
#> # A tibble: 32 × 11
#> mpg cyl disp hp drat wt qsec gear carb vs am
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 4 4 0 1
#> 2 21 6 160 110 3.9 2.88 17.0 4 4 0 1
#> 3 22.8 4 108 93 3.85 2.32 18.6 4 1 1 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 3 1 1 0
#> 5 18.7 8 360 175 3.15 3.44 17.0 3 2 0 0
#> 6 18.1 6 225 105 2.76 3.46 20.2 3 1 1 0
#> 7 14.3 8 360 245 3.21 3.57 15.8 3 4 0 0
#> 8 24.4 4 147. 62 3.69 3.19 20 4 2 1 0
#> 9 22.8 4 141. 95 3.92 3.15 22.9 4 2 1 0
#> 10 19.2 6 168. 123 3.92 3.44 18.3 4 4 1 0
#> # … with 22 more rows
df %>% restore_keys(dplyr::everything(), .remove = TRUE, .unkey = TRUE)
#> # A tibble: 32 × 11
#> mpg cyl disp hp drat wt qsec gear carb vs am
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 4 4 0 1
#> 2 21 6 160 110 3.9 2.88 17.0 4 4 0 1
#> 3 22.8 4 108 93 3.85 2.32 18.6 4 1 1 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 3 1 1 0
#> 5 18.7 8 360 175 3.15 3.44 17.0 3 2 0 0
#> 6 18.1 6 225 105 2.76 3.46 20.2 3 1 1 0
#> 7 14.3 8 360 245 3.21 3.57 15.8 3 4 0 0
#> 8 24.4 4 147. 62 3.69 3.19 20 4 2 1 0
#> 9 22.8 4 141. 95 3.92 3.15 22.9 4 2 1 0
#> 10 19.2 6 168. 123 3.92 3.44 18.3 4 4 1 0
#> # … with 22 more rows
# Restoring on grouped data frame
df_grouped <- df %>% dplyr::mutate(vs = 1) %>% dplyr::group_by(vs)
df_grouped %>% restore_keys(dplyr::everything())
#> # A keyed object. Keys: vs, am
#> # A tibble: 32 × 11
#> # Groups: vs [2]
#> mpg cyl disp hp drat wt qsec gear carb vs am
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 4 4 0 1
#> 2 21 6 160 110 3.9 2.88 17.0 4 4 0 1
#> 3 22.8 4 108 93 3.85 2.32 18.6 4 1 1 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 3 1 1 0
#> 5 18.7 8 360 175 3.15 3.44 17.0 3 2 0 0
#> 6 18.1 6 225 105 2.76 3.46 20.2 3 1 1 0
#> 7 14.3 8 360 245 3.21 3.57 15.8 3 4 0 0
#> 8 24.4 4 147. 62 3.69 3.19 20 4 2 1 0
#> 9 22.8 4 141. 95 3.92 3.15 22.9 4 2 1 0
#> 10 19.2 6 168. 123 3.92 3.44 18.3 4 4 1 0
#> # … with 22 more rows
# Pulling
df %>% pull_key(vs)
#> [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1
# Renaming
df %>% rename_keys(Vs = vs)
#> # A keyed object. Keys: Vs, am
#> # A tibble: 32 × 9
#> mpg cyl disp hp drat wt qsec gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 4 4
#> # … with 22 more rows