nse

looping over a list of filter expressions: problem with NSE in map2 call within mutate

試著忘記壹切 提交于 2019-12-11 07:38:43
问题 I have defined a list of expressions containing arguments I want to pass to a dplyr::filter call. library(tidyverse) # using tidyr 1.0.0 cond_filter <- list(expr(1 > 0), # condition to select all rows expr(Species == "setosa"), expr(Species != "virginica")) I further have a data frame that I put into a list-column and which I then expand by the number of filter expressions in said list. iris_nest <- iris %>% nest(data = everything()) %>% expand_grid(., filters = cond_filter) In a last step I

dplyr::mutate unquote RHS

自作多情 提交于 2019-12-11 05:09:07
问题 I am wondering how to properly UQ string created variable names on the RHS in dplyr methods like mutate . See the error messages I got in comments in the wilcox.test part of this MWE: require(dplyr) dfMain <- data.frame( base = c(rep('A', 5), rep('B', 5)), id = letters[1:10], q0 = rnorm(10) ) backgs <- list( A = rnorm(13), B = rnorm(11) ) fun <- function(dfMain, i = 0){ pcol <- sprintf('p%i', i) qcol <- sprintf('q%i', i) ( dfMain %>% group_by(id) %>% mutate( !!pcol := ifelse( !is.nan(!!qcol)

Creating a function with an argument passed to dplyr::filter what is the best way to work around nse?

有些话、适合烂在心里 提交于 2019-12-09 04:44:38
问题 Non standard evaluation is really handy when using dplyr's verbs. But it can be problematic when using those verbs with function arguments. For example let us say that I want to create a function that gives me the number of rows for a given species. # Load packages and prepare data library(dplyr) library(lazyeval) # I prefer lowercase column names names(iris) <- tolower(names(iris)) # Number of rows for all species nrow(iris) # [1] 150 Example not working This function doesn't work as

Tidy evaluation programming with dplyr::case_when

浪子不回头ぞ 提交于 2019-12-09 02:49:10
问题 I try to write a simple function wrapping around the dplyr::case_when() function. I read the programming with dplyr documentation on https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html but can't figure out how this works with the case_when() function. I have the following data: data <- tibble( item_name = c("apple", "bmw", "bmw") ) And the following list: cat <- list( item_name == "apple" ~ "fruit", item_name == "bmw" ~ "car" ) Then I would like to write a function like:

Tidyeval: pass list of columns as quosure to select()

烈酒焚心 提交于 2019-12-07 14:32:05
问题 I want to pass a bunch of columns to pmap() inside mutate() . Later, I want to select those same columns. At the moment, I'm passing a list of column names to pmap() as a quosure, which works fine, although I have no idea whether this is the "right" way to do it. But I can't figure out how to use the same quosure/list for select() . I've got almost no experience with tidyeval, I've only got this far by playing around. I imagine there must be a way to use the same thing both for pmap() and

How to correctly use dplyr verbs inside a function definition in r?

拈花ヽ惹草 提交于 2019-12-07 14:22:48
问题 I want to use filter and summarise from dplyr inside my function. Without a function it works like following: library(dplyr) > Orange %>% + filter(Tree==1) %>% + summarise(age_max = max(age)) age_max 1 1582 I want to do the same inside a function, but following fails: ## Function definition: df.maker <- function(df, plant, Age){ require(dplyr) dfo <- df %>% filter(plant==1) %>% summarise(age_max = max(Age)) return(dfo) } ## Use: > df.maker(Orange, Tree, age) Rerun with Debug Error in as.lazy

Using dplyr group_by in a function

别说谁变了你拦得住时间么 提交于 2019-12-07 12:14:42
问题 I am trying to use dplyr's group_by in a local function, example: testFunction <- function(df, x) { df %>% group_by(x) %>% summarize(mean.Petal.Width = mean(Petal.Width)) } testFunction(iris, Species) and I get an error "... unknown variable to group by: x" I've tried group_by_ and it gives me a summary of the entire dataset. Anybody have a clue how I can fix this? Thanks in advance! 回答1: Here is one way to work with the new enquo from dplyr , where enquo takes the string and converts to

Extract column name in mutate_if call

只谈情不闲聊 提交于 2019-12-06 03:44:40
问题 I would like to extract the column name in the function call to mutate_if . With this, I then want to look up a value in a different table and fill in missing values with the lookup value. I tried using quosure syntax, but it is not working. Is there a possibility to extract the column name directly? Sample Data df <- structure(list(x = 1:10, y = c(1L, 2L, 3L, NA, 1L, 2L, 3L, NA, 1L, 2L), z = c(NA, 2L, 3L, NA, NA, 2L, 3L, NA, NA, 2L), a = c("a", "b", "c", "d", "e", "a", "b", "c", "d", "e")),

How to correctly use dplyr verbs inside a function definition in r?

依然范特西╮ 提交于 2019-12-05 20:08:40
I want to use filter and summarise from dplyr inside my function. Without a function it works like following: library(dplyr) > Orange %>% + filter(Tree==1) %>% + summarise(age_max = max(age)) age_max 1 1582 I want to do the same inside a function, but following fails: ## Function definition: df.maker <- function(df, plant, Age){ require(dplyr) dfo <- df %>% filter(plant==1) %>% summarise(age_max = max(Age)) return(dfo) } ## Use: > df.maker(Orange, Tree, age) Rerun with Debug Error in as.lazy_dots(list(...)) : object 'Tree' not found I know that similar questions have been asked before. I've

How to use non-standard evaluation NSE to evaluate arguments on data.table?

不问归期 提交于 2019-12-05 02:30:41
问题 Say I have the following library(data.table) cars1 = setDT(copy(cars)) cars2 = setDT(copy(cars)) car_list = list(cars1, cars2) class(car_list) <- "dd" `[.dd` <- function(x,...) { code = rlang::enquos(...) cars1 = x[[1]] rlang::eval_tidy(quo(cars1[!!!code])) } car_list[,.N, by = speed] so I wished to perform arbitrary operations on cars1 and cars2 by defining the [.dd function so that whatever I put into ... get executed by cars1 and cars2 using the [ data.table syntax e.g. car_list[,.N, by =