tidyeval

dplyr: How to use group_by inside a function?

北战南征 提交于 2019-12-02 04:50:52
问题 I want to use use the dplyr::group_by function inside another function, but I do not know how to pass the arguments to this function. Can someone provide a working example? library(dplyr) data(iris) iris %.% group_by(Species) %.% summarise(n = n()) # ## Source: local data frame [3 x 2] ## Species n ## 1 virginica 50 ## 2 versicolor 50 ## 3 setosa 50 mytable0 <- function(x, ...) x %.% group_by(...) %.% summarise(n = n()) mytable0(iris, "Species") # OK ## Source: local data frame [3 x 2] ##

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

Using tidy eval for multiple dplyr filter conditions

这一生的挚爱 提交于 2019-12-01 17:48:17
问题 I'm new to tidy eval and trying to write generic functions- one thing I'm struggling with right now is writing multiple filter conditions for categorical variables. This is what I'm using right now- create_expr <- function(name, val){ if(!is.null(val)) val <- paste0("c('", paste0(val, collapse = "','"), "')") paste(name, "%in%", val) } my_filter <- function(df, cols, conds){ # Args: # df: dataframe which is to be filtered # cols: list of column names which are to be filtered # conds:

dplyr: Standard evaluation and enquo()

浪尽此生 提交于 2019-12-01 01:14:38
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? Colin FAY The idea behind tidyeval is specifically that you don't need to put your column name between "" .

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

Remove columns the tidyeval way

杀马特。学长 韩版系。学妹 提交于 2019-11-30 14:19:17
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 # Hornet 4 Drive 21.4 6 258 110 3.08 # Hornet Sportabout 18.7 8 360 175 3.15 # Valiant 18.1 6 225 105 2.76 I

How to use dplyr's enquo and quo_name in a function with tidyr and ggplot2

回眸只為那壹抹淺笑 提交于 2019-11-30 06:49:14
library(dplyr) #Devel version, soon-to-be-released 0.6.0 library(tidyr) library(ggplot2) library(forcats) #for gss_cat data I'm attempting to write a function that combines quosures from the soon-to-be-released dplyr devel version together with tidyr::gather and ggplot2 . So far it seems to work with tidyr , but I'm having trouble with the plotting. The below function seems to work with tidyr's gather : GatherFun<-function(gath){ gath<-enquo(gath) gss_cat%>%select(relig,marital,race,partyid)%>% gather(key,value,-!!gath)%>% count(!!gath,key,value)%>% mutate(perc=n/sum(n)) } But I can't figure

Using dplyr::quos() with a list argument rather than the ellipsis argument

隐身守侯 提交于 2019-11-29 15:16:18
I am using dplyr and trying to create a function to calculate p.values based on grouping arguments. I would like to be able to have an argument that would be list of any length of variables to group by. Here is the example dataset: dataset <- structure(list(Experiment = c(170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222,

Supplying multiple groups of variables to a function for dplyr arguments in the body

狂风中的少年 提交于 2019-11-29 14:20:10
Here is the data: library(tidyverse) data <- tibble::tribble( ~var1, ~var2, ~var3, ~var4, ~var5, "a", "d", "g", "hello", 1L, "a", "d", "h", "hello", 2L, "b", "e", "h", "k", 4L, "b", "e", "h", "k", 7L, "c", "f", "i", "hello", 3L, "c", "f", "i", "hello", 4L ) and the vectors, I want to use: filter_var <- c("hello") groupby_vars1 <- c("var1", "var2", "var3") groupby_vars2 <- c("var1", "var2") joinby_vars1 <- c("var1", "var2") joinby_vars2 <- c("var1", "var2", "var3") 2nd & 5th, and 3rd & 4th vectors are same, but please assume they are different and retain them as different vectors. Now I want to

Tidyeval with list of column names in a function

血红的双手。 提交于 2019-11-29 12:51:30
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 of the function, the above solution will not work (of course): my_summarise <- function(df, group_var,