quosure

Accept both bare (from rlang) or string as a function input

你。 提交于 2020-06-12 08:40:26
问题 I editing an existing function in a package. Currently, the function accepts a column name in a data frame as a string. I am updating the function to accept either a string name or a bare name. But I am running into some issues. The general approach I'd like to take is to convert a bare to a string, so the rest of the function does not need to be updated. If the user passes a string column name, then I don't need to modify the input. The code below converts a bare input to a string, but I can

dplyr mutate using variable columns

家住魔仙堡 提交于 2020-01-03 16:57:17
问题 I am trying to use mutate to create a new column with values based on a specific column. Example final data frame (I am trying to create new_col ): x = tibble(colA = c(11, 12, 13), colB = c(91, 92, 93), col_to_use = c("colA", "colA", "colB"), new_col = c(11, 12, 93)) I would like to do something like: x %>% mutate(new_col = col_to_use) Except instead of column contents, I would like to transform them to a variable. I started with: col_name = "colA" x %>% mutate(new_col = !!as.name(col_name))

Function which runs lm over different variables

六月ゝ 毕业季﹏ 提交于 2020-01-02 05:22:55
问题 I would like to create a function which can run a regression model (e.g. using lm) over different variables in a given dataset. In this function, I would specify as arguments the dataset I'm using, the dependent variable y and the independent variable x. I want this to be a function and not a loop as I would like to call the code in various places of my script. My naive function would look something like this: lmfun <- function(data, y, x) { lm(y ~ x, data = data) } This function obviously

Passing an expression into `MoreArgs` of `mapply`

和自甴很熟 提交于 2019-12-13 17:20:02
问题 I'm doing some programming using dplyr , and am curious about how to pass an expression as (specifically a MoreArgs ) argument to mapply ? Consider a simple function F that subsets a data.frame based on some ids and a time_range , then outputs a summary statistic based on some other column x . require(dplyr) F <- function(ids, time_range, df, date_column, x) { date_column <- enquo(date_column) x <- enquo(x) df %>% filter(person_id %chin% ids) %>% filter(time_range[1] <= (!!date_column) & (!

Great than sign and quosure producing error

北城余情 提交于 2019-12-11 07:16:53
问题 I am encountering an issue when using dplyr in a function. When filtering based on a quosure, the > sign appears to cause an issue where no data are returned. For example: temp_df <- data.frame( startdate_c = as.Date(c("2011-08-08", "2007-09-01", "2012-01-01", "2012-10-26", "2012-12-01", "2016-01-01", "2006-06-01", "2009-04-01", "2005-02-09", "2004-08-01")), enddate_c = as.Date(c("2011-08-14", "2012-09-04", "2014-06-06", "2014-02-28", "2013-04-05", "2016-12-01", "2008-04-18", "2009-08-16",

using non-standard evaluation with formula

丶灬走出姿态 提交于 2019-12-08 13:24:39
问题 I'm creating a package that uses non-standard evaluation to keep track of the meaning of columns. The package passes a data frame among functions, which do various things do the same set of columns. Nonstandard evaluation works great for this: my_select <- function(df, xcol, ycol) { new_df <- dplyr::select(df, !!xcol, !!ycol) new_df } my_select(mtcars, quo(wt), quo(mpg)) However, I'd like a function that works with a formula: my_lm <- function(df, xcol, ycol) { new_lm <- lm(!!xcol, !!ycol,

How to pass multiple group_by arguments and a dynamic variable argument to a dplyr function

坚强是说给别人听的谎言 提交于 2019-12-08 08:20:33
I am trying to pass multiple group_by arguments to a dplyr function as well as a named variable. In understand that I need to use a quosure for dplyr to understand the variables i am passing to it. The following code works fine: quantileMaker2 <- function(data, groupCol, calcCol) { groupCol <- enquo(groupCol) calcCol <- enquo(calcCol) data %>% group_by(!! groupCol) %>% summarise('25%' = currency(quantile(!! calcCol, probs = 0.25), digits = 2L), '50%' = currency(quantile(!! calcCol, probs = 0.50), digits = 2L), '75%' = currency(quantile(!! calcCol, probs = 0.75), digits = 2L), avg = currency

How to pass multiple group_by arguments and a dynamic variable argument to a dplyr function

旧巷老猫 提交于 2019-12-08 05:25:13
问题 I am trying to pass multiple group_by arguments to a dplyr function as well as a named variable. In understand that I need to use a quosure for dplyr to understand the variables i am passing to it. The following code works fine: quantileMaker2 <- function(data, groupCol, calcCol) { groupCol <- enquo(groupCol) calcCol <- enquo(calcCol) data %>% group_by(!! groupCol) %>% summarise('25%' = currency(quantile(!! calcCol, probs = 0.25), digits = 2L), '50%' = currency(quantile(!! calcCol, probs = 0

Function which runs lm over different variables

北城余情 提交于 2019-12-05 17:40:12
I would like to create a function which can run a regression model (e.g. using lm) over different variables in a given dataset. In this function, I would specify as arguments the dataset I'm using, the dependent variable y and the independent variable x. I want this to be a function and not a loop as I would like to call the code in various places of my script. My naive function would look something like this: lmfun <- function(data, y, x) { lm(y ~ x, data = data) } This function obviously does not work because the lm function does not recognize y and x as variables of the dataset. I have done