Description Usage Arguments Details Value See Also Examples
These functions power [select()] and [rename()].
1 2 3 | vars_select(.vars, ..., .include = character(), .exclude = character())
vars_rename(.vars, ..., .strict = TRUE)
|
.vars |
A character vector of existing column names. |
..., args |
Expressions to compute These arguments are automatically [quoted][rlang::quo] and [evaluated][rlang::eval_tidy] in a context where elements of 'vars' are objects representing their positions within 'vars'. They support [unquoting][rlang::quasiquotation] and splicing. See 'vignette("programming")' for an introduction to these concepts. Note that except for ':', '-' and 'c()', all complex expressions are evaluated outside that context. This is to prevent accidental matching to 'vars' elements when you refer to variables from the calling context. |
.include, .exclude |
Character vector of column names to always include/exclude. |
.strict |
If 'TRUE', will throw an error if you attempt to rename a variable that doesn't exist. |
For historic reasons, the 'vars' and 'include' arguments are not prefixed with '.'. This means that any argument starting with 'v' might partial-match on 'vars' if it is not explicitly named. Also '...' cannot accept arguments named 'exclude' or 'include'. You can enquose and splice the dots to work around these limitations (see examples).
A named character vector. Values are existing column names, names are new names.
[vars_pull()]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | # Keep variables
vars_select(names(iris), everything())
vars_select(names(iris), starts_with("Petal"))
vars_select(names(iris), ends_with("Width"))
vars_select(names(iris), contains("etal"))
vars_select(names(iris), matches(".t."))
vars_select(names(iris), Petal.Length, Petal.Width)
vars_select(names(iris), one_of("Petal.Length", "Petal.Width"))
df <- as.data.frame(matrix(runif(100), nrow = 10))
df <- df[c(3, 4, 7, 1, 9, 8, 5, 2, 6, 10)]
vars_select(names(df), num_range("V", 4:6))
# Drop variables
vars_select(names(iris), -starts_with("Petal"))
vars_select(names(iris), -ends_with("Width"))
vars_select(names(iris), -contains("etal"))
vars_select(names(iris), -matches(".t."))
vars_select(names(iris), -Petal.Length, -Petal.Width)
# Rename variables
vars_select(names(iris), petal_length = Petal.Length)
vars_select(names(iris), petal = starts_with("Petal"))
# Rename variables preserving all existing
vars_rename(names(iris), petal_length = Petal.Length)
# You can unquote names or formulas (or lists of)
vars_select(names(iris), !!! list(quo(Petal.Length)))
vars_select(names(iris), !! quote(Petal.Length))
# The .data pronoun is available:
vars_select(names(mtcars), .data$cyl)
vars_select(names(mtcars), .data$mpg : .data$disp)
# However it isn't available within calls since those are evaluated
# outside of the data context. This would fail if run:
# vars_select(names(mtcars), identical(.data$cyl))
# If you're writing a wrapper around vars_select(), pass the dots
# via splicing to avoid matching dotted arguments to vars_select()
# named arguments (`vars`, `include` and `exclude`):
wrapper <- function(...) {
vars_select(names(mtcars), !!! quos(...))
}
# This won't partial-match on `vars`:
wrapper(var = cyl)
# This won't match on `include`:
wrapper(include = cyl)
# If your wrapper takes named arguments, you need to capture then
# unquote to pass them to vars_select(). See the vignette on
# programming with dplyr for more on this:
wrapper <- function(var1, var2) {
vars_select(names(mtcars), !! enquo(var1), !! enquo(var2))
}
wrapper(starts_with("d"), starts_with("c"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.