Last chance! 50% off unlimited learning
Sale ends in
select()
keeps only the variables you mention; rename()
keeps all variables.select(.data, ...)select_(.data, ..., .dots)
rename(.data, ...)
rename_(.data, ...)
select_()
to do standard evaluation. See
vignette("nse")
for details.data
.Data frame row names are silently dropped. To preserve, convert to an explicit variable.
As well as using existing functions like :
and c
, there are
a number of special functions that only work inside select
starts_with(x, ignore.case = TRUE)
:
names starts with x
ends_with(x, ignore.case = TRUE)
:
names ends in x
contains(x, ignore.case = TRUE)
:
selects all variables whose name contains x
matches(x, ignore.case = TRUE)
:
selects all variables whose name matches the regular expression x
num_range("x", 1:5, width = 2)
:
selects all variables (numerically) from x01 to x05.
one_of("x", "y", "z")
:
selects variables provided in a character vector.
everything()
:
selects all variables.
To drop variables, use -
. You can rename variables with
named arguments.
arrange
,
arrange_
; filter
,
filter_
; mutate
,
mutate_
, transmute
,
transmute_
; slice
,
slice_
; summarise
,
summarise_
, summarize
,
summarize_
iris <- tbl_df(iris) # so it prints a little nicer
select(iris, starts_with("Petal"))
select(iris, ends_with("Width"))
select(iris, contains("etal"))
select(iris, matches(".t."))
select(iris, Petal.Length, Petal.Width)
vars <- c("Petal.Length", "Petal.Width")
select(iris, one_of(vars))
df <- as.data.frame(matrix(runif(100), nrow = 10))
df <- tbl_df(df[c(3, 4, 7, 1, 9, 8, 5, 2, 6, 10)])
select(df, V4:V6)
select(df, num_range("V", 4:6))
# Drop variables
select(iris, -starts_with("Petal"))
select(iris, -ends_with("Width"))
select(iris, -contains("etal"))
select(iris, -matches(".t."))
select(iris, -Petal.Length, -Petal.Width)
# Rename variables:
# * select() keeps only the variables you specify
select(iris, petal_length = Petal.Length)
# * rename() keeps all variables
rename(iris, petal_length = Petal.Length)
# Programming with select ---------------------------------------------------
select_(iris, ~Petal.Length)
select_(iris, "Petal.Length")
select_(iris, lazyeval::interp(~matches(x), x = ".t."))
select_(iris, quote(-Petal.Length), quote(-Petal.Width))
select_(iris, .dots = list(quote(-Petal.Length), quote(-Petal.Width)))
Run the code above in your browser using DataLab