nse

group_by by a vector of characters using tidy evaluation semantics

自闭症网瘾萝莉.ら 提交于 2020-01-15 11:07:46
问题 I used to do it, using group_by_ library(dplyr) group_by <- c('cyl', 'vs') mtcars %>% group_by_(.dots = group_by) %>% summarise(gear = mean(gear)) but now group_by_ is deprecated. I don't know how to do it using the tidy evaluation framework. 回答1: There is group_by_at variant of group_by : library(dplyr) group_by <- c('cyl', 'vs') mtcars %>% group_by_at(group_by) %>% summarise(gear = mean(gear)) Above it's simplified version of generalized: mtcars %>% group_by_at(vars(one_of(group_by))) %>%

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))

rlang: Get names from … with colon shortcut in NSE function

醉酒当歌 提交于 2019-12-30 07:06:43
问题 I'm writing a package of functions for making tables of demographics data. I have one function, abbreviated below, where I need to take in several columns ( ... ) on which I'll gather a data frame. The trick is I'd like to keep those columns' names in order, because I'll need to put a column in that order after gathering. In this case, those columns are estimate , moe , share , sharemoe . library(tidyverse) library(rlang) race <- structure(list(region = c("New Haven", "New Haven", "New Haven"

Rename in dplyr 0.7+ function

对着背影说爱祢 提交于 2019-12-29 08:48:29
问题 I am having difficulty renaming columns in a function using dplyr. I have already found helpful posts on nonstandard evaluation and the use of enquo (e.g., http://dplyr.tidyverse.org/articles/programming.html and Changing names of resulting variables in custom dplyr function). The ultimate goal is to use the function to summarise each group, and then rename the columns into something more meaningful than the original variable names. Here is a simple example, which works except for the

Using dplyr within a function, non-standard evaluation

喜你入骨 提交于 2019-12-28 04:21:07
问题 Trying to get my head around Non-Standard Evaluation as used by dplyr but without success. I'd like a short function that returns summary statistics (N, mean, sd, median, IQR, min, max) for a specified set of variables. Simplified version of my function... my_summarise <- function(df = temp, to.sum = 'eg1', ...){ ## Summarise results <- summarise_(df, n = ~n(), mean = mean(~to.sum, na.rm = TRUE)) return(results) } And running it with some dummy data... set.seed(43290) temp <- cbind(rnorm(n =

R make function robust to both standard and non-standard evaluation

本秂侑毒 提交于 2019-12-24 01:08:30
问题 I have a little function that searches though user defined column of a dataframe relying on dplyr . In the current form below it accepts the column argument in non-standard evaluation - without quotes (e.g. scenario instead of "scenario" in standard evaluation). search_column <- function(df, column, string, fixed = TRUE){ df <- dplyr::select_(df, deparse(substitute(column))) df <- distinct(df) return(grep(string, df[[1]], fixed = fixed, value = TRUE)) } Is there a way to make the function

Why do quosures work in group_by() but not filter()?

我只是一个虾纸丫 提交于 2019-12-23 11:58:18
问题 I'm working on building a function that I will manipulate a data frame based on a string. Within the function, I'll build a column name as from the string and use it to manipulate the data frame, something like this: library(dplyr) orig_df <- data_frame( id = 1:3 , amt = c(100, 200, 300) , anyA = c(T,F,T) , othercol = c(F,F,T) ) summarize_my_df_broken <- function(df, my_string) { my_column <- quo(paste0("any", my_string)) df %>% filter(!!my_column) %>% group_by(othercol) %>% summarize( n = n(

R: Using a string as an argument to mutate verb in dplyr

偶尔善良 提交于 2019-12-17 16:30:49
问题 I am building a shiny app which needs to allow users to define new variables for plotting. Specifically I want to allow users to define an expression to be used in mutate verb. The server receives the expression as text and I am wondering how to make mutate execute it in dplyr 0.7. I can make it work (partially) using mutate_ but it is deprecated now. It also defines the new column name as the entire expression rather than the new variable Here is a reproducible example: input_from_shiny <-

Why is enquo + !! preferable to substitute + eval

前提是你 提交于 2019-12-17 09:33:15
问题 In the following example, why should we favour using f1 over f2 ? Is it more efficient in some sense? For someone used to base R, it seems more natural to use the "substitute + eval" option. library(dplyr) d = data.frame(x = 1:5, y = rnorm(5)) # using enquo + !! f1 = function(mydata, myvar) { m = enquo(myvar) mydata %>% mutate(two_y = 2 * !!m) } # using substitute + eval f2 = function(mydata, myvar) { m = substitute(myvar) mydata %>% mutate(two_y = 2 * eval(m)) } all.equal(d %>% f1(y), d %>%

dplyr concat columns stored in variable (mutate and non standard evaluation)

[亡魂溺海] 提交于 2019-12-12 15:37:38
问题 I would like to concatenate an arbitrary number of columns in a dataframe based on a variable cols_to_concat df <- dplyr::data_frame(a = letters[1:3], b = letters[4:6], c = letters[7:9]) cols_to_concat = c("a", "b", "c") To achieve the desired result with this specific value of cols_to_concat I could do this: df %>% dplyr::mutate(concat = paste0(a, b, c)) But I need to generalise this, using syntax a bit like this # (DOES NOT WORK) df %>% dplyr::mutate(concat = paste0(cols)) I'd like to use