tidyeval

Tidyeval with list of column names in a function

余生颓废 提交于 2019-12-29 08:20:26
问题 I am trying to create a function that passes a list of column names to a dplyr function. I know how to do this if the list of columns names is given in the ... form, as explained in the tidyeval documentation: df <- tibble( g1 = c(1, 1, 2, 2, 2), g2 = c(1, 2, 1, 2, 1), a = sample(5), b = sample(5) ) my_summarise <- function(df, ...) { group_var <- quos(...) df %>% group_by(!!!group_var) %>% summarise(a = mean(a)) } my_summarise(df, g1, g2) But if I want to list the column names as an argument

Non-standard evaluation and quasiquotation in dplyr() not working as (naively) expected

不想你离开。 提交于 2019-12-29 00:41:09
问题 I am trying to search a database and then label the ouput with a name derived from the original search, "derived_name" in the reproducible example below. I am using a dplyr pipe %>% , and I am having trouble with quasiquotation and/or non-standard evaluation. Specifically, using count_colname , a character object derived from "derived_name" , in the final top_n() function fails to subset the dataframe. search_name <- "derived_name" set.seed(1) letrs <- letters[rnorm(52, 13.5, 5)] letrs_count

Non-standard evaluation and quasiquotation in dplyr() not working as (naively) expected

亡梦爱人 提交于 2019-12-29 00:41:06
问题 I am trying to search a database and then label the ouput with a name derived from the original search, "derived_name" in the reproducible example below. I am using a dplyr pipe %>% , and I am having trouble with quasiquotation and/or non-standard evaluation. Specifically, using count_colname , a character object derived from "derived_name" , in the final top_n() function fails to subset the dataframe. search_name <- "derived_name" set.seed(1) letrs <- letters[rnorm(52, 13.5, 5)] letrs_count

How to combine ggplot and dplyr into a function?

只愿长相守 提交于 2019-12-28 04:08:06
问题 Consider this simple example library(dplyr) library(ggplot2) dataframe <- data_frame(id = c(1,2,3,4), group = c('a','b','c','c'), value = c(200,400,120,300)) # A tibble: 4 x 3 id group value <dbl> <chr> <dbl> 1 1 a 200 2 2 b 400 3 3 c 120 4 4 c 300 Here I want to write a function that takes the dataframe and the grouping variable as input. Ideally, after grouping and aggregating I would like to print a ggpplot chart. This works: get_charts2 <- function(data, mygroup){ quo_var <- enquo(mygroup

Pass data frame columns as arguments to mutate function

喜夏-厌秋 提交于 2019-12-23 21:32:04
问题 I have one table with five columns Year, GDP, Revenue, Income and Wages.With this table I made calculation with code below. library(dplyr) #DATA TEST<-data.frame( Year= c(2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021), GDP =c(8634,5798,6022,6002,6266,6478,6732,7224,6956,6968,7098,7620,7642,8203,9856,20328,22364,22222,23250,25250,26250,27250), Revenue =c(8734,5798,7011,7002,7177,7478,7731,7114,7957,7978,7098,7710,7742,8203,9857

enquo() inside a magrittr pipeline

我是研究僧i 提交于 2019-12-23 19:24:50
问题 I just would like to understand what's going wrong here. In the first case (working), I assign the enquo() -ted argument to a variable, in the second case, I use the enquoted argument directly in my call to mutate . library("dplyr") df <- tibble(x = 1:5, y= 1:5, z = 1:5) # works myfun <- function(df, transformation) { my_transformation <- rlang::enquo(transformation) df %>% gather("key","value", x,y,z) %>% mutate(value = UQ(my_transformation)) } myfun(df,exp(value)) # does not work myfun_2 <-

Passing labels to xlab and ylab in ggplot2

╄→гoц情女王★ 提交于 2019-12-20 02:45:21
问题 I have created a function where the objective is to create a series of plots in a vectorzation way. The functions partially does what I want which is update update the plot based on the selected variables. However, I am not able to pass the label argument (i.e. label_x and label_y) so that the xlab and ylab are updated consistently. library(tidyverse) plot_scatter_with_label <- function(df, var_x, var_y, label_x, label_y, geom_smooth = FALSE, point_shape = 16, point_color = "#EB3300", point

dplyr: Standard evaluation and enquo()

亡梦爱人 提交于 2019-12-19 04:56:46
问题 I heard standard evaluation is not recommended in dplyr, and we can do similar thing with enquo() and quo() . My original code (simplified) is my_function <- function(data, x="OriginalX", y="OriginalY"){ data %>% mutate_(CopyX = x, CopyY = y) } and it works. I tried following code my_function <- function(data, x="OriginalX", y="OriginalY"){ qx <- enquo(x) qy <- enquo(y) data %>% mutate(CopyX = (!!qx), CopyY = (!!qy)) } Why it does not work? And should we keep using standard evaluation? 回答1:

Remove columns the tidyeval way

百般思念 提交于 2019-12-18 16:48:40
问题 I would like to remove a vector of columns using dplyr >= 0.7 library(dplyr) data(mtcars) rem_cols <- c("wt", "qsec", "vs", "am", "gear", "carb") head(select(mtcars, !!paste0("-", rem_cols))) Error: Strings must match column names. Unknown columns: -wt, -qsec, -vs, -am, -gear, -carb dplyr < 0.7 worked as follows: head(select_(mtcars, .dots = paste0("-", rem_cols))) # mpg cyl disp hp drat # Mazda RX4 21.0 6 160 110 3.90 # Mazda RX4 Wag 21.0 6 160 110 3.90 # Datsun 710 22.8 4 108 93 3.85 #

How to use tidyeval on a column to mutate?

那年仲夏 提交于 2019-12-18 07:05:25
问题 I'm sorry for the confusion but eventually, the first example I posted (at the bottom of the page), did not help me to figure out how tidyeval works with mutate, so I'm adding a new example. I would like to create a function that takes three args: a dataframe the column(s) to mutate a variable (from the dataframe) to replace the values that are being mutated For instance, to replace the values in mpg with the values from carb I tried this: I tried this colToX <- function(dt, ..., repl) { cols