quosure

R & quosures - How to get names of symbols contained in a vector passed as function argument?

醉酒当歌 提交于 2021-02-09 08:56:28
问题 I want to write an R function arg2str that returns the names (that is a vector of strings) of the symbols that are fed as arguments. For the most simple case, I only have one input symbol: library ("rlang") arg2str.v0 <- function (arg) rlang::quo_name (enquo (arg)) arg2str.v0 (a) ## [1] "a" If I have multiple symbols, I can use the three-dots construct: arg2str.v1 <- function (...) sapply (enquos (...), rlang::quo_name) arg2str.v1 (a, b, c) ## ## "a" "b" "c" (Subsidiary question: why is the

using quosures within formula inside an anonymous function

Deadly 提交于 2021-02-08 19:43:49
问题 I am trying to use quosures to pass along variable names within a custom function for data processing and use in a formula, but my use of quosures in the formula is not correct. Is there a better way to unquote arguments within a formula? library(dplyr) library(broom) library(purrr) library(tidyr) foo <- function(mydata, dv, iv, group_var) { dv = enquo(dv) iv = enquo(iv) group_var = enquo(group_var) mydata <- mydata %>% group_by(!!group_var) %>% nest() mydata %>% mutate(model = map(data,

using quosures within formula inside an anonymous function

自古美人都是妖i 提交于 2021-02-08 19:42:22
问题 I am trying to use quosures to pass along variable names within a custom function for data processing and use in a formula, but my use of quosures in the formula is not correct. Is there a better way to unquote arguments within a formula? library(dplyr) library(broom) library(purrr) library(tidyr) foo <- function(mydata, dv, iv, group_var) { dv = enquo(dv) iv = enquo(iv) group_var = enquo(group_var) mydata <- mydata %>% group_by(!!group_var) %>% nest() mydata %>% mutate(model = map(data,

Passing column name as parameter to a function using dplyr

天大地大妈咪最大 提交于 2021-02-08 02:12:06
问题 I have a dataframe like below : transid<-c(1,2,3,4,5,6,7,8) accountid<-c(a,a,b,a,b,b,a,b) month<-c(1,1,1,2,2,3,3,3) amount<-c(10,20,30,40,50,60,70,80) transactions<-data.frame(transid,accountid,month,amount) I am trying to write function for total monthly amount for each accountid using dplyr package verbs. my_sum<-function(df,col1,col2,col3){ df %>% group_by_(col1,col2) %>%summarise_(total_sum = sum(col3)) } my_sum(transactions, "accountid","month","amount") To get the result like below:

Passing column name as parameter to a function using dplyr

吃可爱长大的小学妹 提交于 2021-02-08 02:04:49
问题 I have a dataframe like below : transid<-c(1,2,3,4,5,6,7,8) accountid<-c(a,a,b,a,b,b,a,b) month<-c(1,1,1,2,2,3,3,3) amount<-c(10,20,30,40,50,60,70,80) transactions<-data.frame(transid,accountid,month,amount) I am trying to write function for total monthly amount for each accountid using dplyr package verbs. my_sum<-function(df,col1,col2,col3){ df %>% group_by_(col1,col2) %>%summarise_(total_sum = sum(col3)) } my_sum(transactions, "accountid","month","amount") To get the result like below:

How to pass a filter statement as a function parameter in dplyr using quosure [duplicate]

白昼怎懂夜的黑 提交于 2021-02-05 07:55:09
问题 This question already has answers here : dplyr/rlang: parse_expr with multiple expressions (3 answers) Closed 9 months ago . Using the dplyr package in R , I want to pass a filter statement as a parameter in a function. I don't know how to evaluate the statement as code instead of a string. When I try the code below, I get an error message. I'm assuming I need a quosure or something, but I don't fully grasp that concept. data("PlantGrowth") myfunc <- function(df, filter_statement) { df %>%

How do I pass a dynamic variable name created using enquo() to dplyr's mutate for evaluation?

人走茶凉 提交于 2021-02-05 07:26:05
问题 I'm creating a workflow that contains the same piping steps of renaming, selecting by, then mutating all using a name I provide prior to the pipe. I have had success using enquo() and !! (bang bang) to rename to my desired string and then select it again, but when I reach the mutate step it either repeats the text string as the column values or will not evaluate. I've recreated the code below: #Testing rename, select, and mutate use cases for enquo() #Load packages library(dplyr) library

dplyr case_when with dynamic number of cases

蹲街弑〆低调 提交于 2021-01-27 07:23:41
问题 Wanting to use dplyr and case_when to collapse a series of indicator columns into a single column. The challenge is I want to be able to collapse over an unspecified/dynamic number of columns. Consider the following dataset, gear has been split into a series of indicator columns. library(dplyr) data(mtcars) mtcars = mtcars %>% mutate(g2 = ifelse(gear == 2, 1, 0), g3 = ifelse(gear == 3, 1, 0), g4 = ifelse(gear == 4, 1, 0)) %>% select(g2, g3, g4) I am trying to write a function that does the

dplyr case_when with dynamic number of cases

五迷三道 提交于 2021-01-27 07:23:04
问题 Wanting to use dplyr and case_when to collapse a series of indicator columns into a single column. The challenge is I want to be able to collapse over an unspecified/dynamic number of columns. Consider the following dataset, gear has been split into a series of indicator columns. library(dplyr) data(mtcars) mtcars = mtcars %>% mutate(g2 = ifelse(gear == 2, 1, 0), g3 = ifelse(gear == 3, 1, 0), g4 = ifelse(gear == 4, 1, 0)) %>% select(g2, g3, g4) I am trying to write a function that does the