rlang

Correct usage of dplyr::select in dplyr 0.7.0+, selecting columns using character vector

耗尽温柔 提交于 2019-12-03 14:01:57
Suppose we have a character vector cols_to_select containing some columns we want to select from a dataframe df , e.g. df <- tibble::data_frame(a=1:3, b=1:3, c=1:3, d=1:3, e=1:3) cols_to_select <- c("b", "d") Suppose also we want to use dplyr::select because it's part of an operation that uses %>% so using select makes the code easy to read. There seem to be a number of ways which this can be achieved, but some are more robust than others. Please could you let me know which is the 'correct' version and why? Or perhaps there is another, better way? dplyr::select(df, cols_to_select) #Fails if

R quo_name equivalent of quos

时光怂恿深爱的人放手 提交于 2019-12-03 12:33:42
Hi following Programming with dplyr I noticed that one can add a name using quo_name. I was wondering how to do this for multiple columns, eg. like a quos_name of sorts. E.g.: my_mutate <- function(df, expr) { expr <- enquo(expr) mean_name <- paste0("mean_", quo_name(expr)) sum_name <- paste0("sum_", quo_name(expr)) mutate(df, !!mean_name := mean(!!expr), !!sum_name := sum(!!expr) ) } becomes my_mutate <- function(df, ...) { exprs <-quos(...) mean_names <- paste0("mean_", quos_name(exprs)) sum_names <- paste0("sum_", quos_name(exprs)) mutate(df, !!!mean_names := mean(!!!exprs), !!!sum_names :=

Tidy evaluation programming and ggplot2

心不动则不痛 提交于 2019-12-03 12:21:08
Trying to write a relatively simple wrapper to produce some plots, but can not work out how to specify tidy evaluation of grouping variables specified as ... an example function that facets variables but doesn't distinguish by grouping... my_plot <- function(df = starwars, select = c(height, mass), ...){ results <- list() ## Tidyeval arguments quo_select <- enquo(select) quo_group <- quos(...) ## Filter, reshape and plot results$df <- df %>% dplyr::filter(!is.na(!!!quo_group)) %>% dplyr::select(!!quo_select, !!!quo_group) %>% gather(key = variable, value = value, !!!quo_select) %>% ## Specify

How to feed a list of unquoted column names into `lapply` (so that I can use it with a `dplyr` function)

耗尽温柔 提交于 2019-12-03 06:20:08
I am trying to write a function in tidyverse/dplyr that I want to eventually use with lapply (or map ). (I had been working on it to answer this question , but came upon an interesting result/dead-end. Please don't mark this as a duplicate - this question is an extension/departure from the answers that you see there.) Is there 1) a way to get a list of quoted variables to work inside a dplyr function (and not use the deprecated SE_ functions) or is there 2) some way to feed a list of unquoted strings through an lapply or map I have used the Programming in Dplyr vignette to construct what I

How to evaluate a constructed string with non-standard evaluation using dplyr?

拥有回忆 提交于 2019-12-02 00:11:02
问题 I have read several guides on programming with dplyr now and I am still confused about how to solve the problem of evaluating constructed/concatenated strings with non-standard evaluation (NSE). I realize that there are better ways to solve this example than using NSE, but want to learn how to. t <- tibble( x_01 = c(1, 2, 3), x_02 = c(4, 5, 6)) i <- 1 This is my desired outcome but want the variables in mutate() to be constructed: t %>% mutate(d_01 = x_01 * 2) #> A tibble: 3 x 3 #> x_01 x_02

How to use dplyr programming syntax to create and evaluate variable names

牧云@^-^@ 提交于 2019-12-01 18:05:58
I would like to dynamically input a variable name using dplyr programming syntax, however, as many have described this can be quite confusing. I've played around with various combinations of quo/enquo !! etc. to no avail. Here is the simplest form of my code library(tidyverse) df <- tibble( color1 = c("blue", "blue", "blue", "blue", "blue"), color2 = c("black", "black", "black", "black", "black"), value = 1:5 ) num <- 2 df %>% mutate(color3 = !!(paste0("color", num))) #> # A tibble: 5 x 4 #> color1 color2 value color3 #> <chr> <chr> <int> <chr> #> 1 blue black 1 color2 #> 2 blue black 2 color2

How to use dplyr programming syntax to create and evaluate variable names

安稳与你 提交于 2019-12-01 17:27:53
问题 I would like to dynamically input a variable name using dplyr programming syntax, however, as many have described this can be quite confusing. I've played around with various combinations of quo/enquo !! etc. to no avail. Here is the simplest form of my code library(tidyverse) df <- tibble( color1 = c("blue", "blue", "blue", "blue", "blue"), color2 = c("black", "black", "black", "black", "black"), value = 1:5 ) num <- 2 df %>% mutate(color3 = !!(paste0("color", num))) #> # A tibble: 5 x 4 #>

R dplyr operate on a column known only by its string name

烈酒焚心 提交于 2019-12-01 12:33:53
I am wrestling with programming using dplyr in R to operate on columns of a data frame that are only known by their string names. I know there was recently an update to dplyr to support quosures and the like and I've reviewed what I think are the relevant components of the new "Programming with dplyr" article here: http://dplyr.tidyverse.org/articles/programming.html . However, I'm still not able to do what I want. My situation is that I know a column name of a data frame only by its string name. Thus, I can't use non-standard evaluation in a call to dplyr within a function or even a script

R dplyr operate on a column known only by its string name

∥☆過路亽.° 提交于 2019-12-01 11:18:45
问题 I am wrestling with programming using dplyr in R to operate on columns of a data frame that are only known by their string names. I know there was recently an update to dplyr to support quosures and the like and I've reviewed what I think are the relevant components of the new "Programming with dplyr" article here: http://dplyr.tidyverse.org/articles/programming.html. However, I'm still not able to do what I want. My situation is that I know a column name of a data frame only by its string

rlang::sym in anonymous functions

无人久伴 提交于 2019-11-30 22:22:40
问题 I recently notices that rlang::sym doesn't seem to work in anonymous functions and I don't understand why. Here an example, it's pretty clumsy and ugly but I think it illustrates the point require(tidyverse) data <- tibble(x1 = letters[1:3], x2 = letters[4:6], val = 1:3) get_it <- function(a, b){ data %>% mutate(y1 = !!rlang::sym(a)) %>% mutate(y2 = !!rlang::sym(b)) %>% select(y1, y2, val) } get_it("x1", "x2") This defines some toy data and a (horrible) function that essentially renames the