R/select_vars.R

Defines functions select_vars

Documented in select_vars

#' Select variables from a dataset
#'
#' Select a (reordered) subset of variables from a subset.
#'
#' @param data the dataset
#' @param keep vector of variable names to keep
#' @return a data frame with tidyverse code attribute
#' @rdname select_vars
#' @author Tom Elliott, Zhaoming Su
#' @examples
#' select_vars(iris, c("Sepal.Length", "Species", "Sepal.Width"))
#' @export
select_vars <- function(data, keep) {
    expr <- rlang::enexpr(data)
    if (is_survey(data)) {
        expr <- coerce_tbl_svy(expr, data)
        expr <- rlang::expr(!!expr %>% srvyr::select(!!!rlang::syms(keep)))
    } else {
        expr <- rlang::expr(!!expr %>% dplyr::select(!!!rlang::syms(keep)))
    }
    eval_code(expr)
}

Try the iNZightTools package in your browser

Any scripts or data that you put into this service are public.

iNZightTools documentation built on Oct. 12, 2023, 5:06 p.m.