nse

rlang::sym in anonymous functions

烈酒焚心 提交于 2019-12-05 01:01:16
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 columns based on column names. Now I can do the same thing for different combinations of a and b: d <-

R quo_name equivalent of quos

試著忘記壹切 提交于 2019-12-04 19:23:42
问题 Hi following Programming with dplyr I noticed that one can add a name using quo_name. I was wondering how to do this for multiple columns, eg. like a quos_name of sorts. E.g.: my_mutate <- function(df, expr) { expr <- enquo(expr) mean_name <- paste0("mean_", quo_name(expr)) sum_name <- paste0("sum_", quo_name(expr)) mutate(df, !!mean_name := mean(!!expr), !!sum_name := sum(!!expr) ) } becomes my_mutate <- function(df, ...) { exprs <-quos(...) mean_names <- paste0("mean_", quos_name(exprs))

use dplyr mutate() in programming

╄→尐↘猪︶ㄣ 提交于 2019-12-04 14:06:50
问题 I am trying to assign a column name to a variable using mutate. df <-data.frame(x = sample(1:100, 50), y = rnorm(50)) new <- function(name){ df%>%mutate(name = ifelse(x <50, "small", "big")) } When I run new(name = "newVar") it doesn't work. I know mutate_() could help but I'm struggling in using it together with ifelse . Any help would be appreciated. 回答1: Using dplyr 0.7.1 and its advances in NSE, you have to UQ the argument to mutate and then use := when assigning. There is lots of info on

R quo_name equivalent of quos

时光怂恿深爱的人放手 提交于 2019-12-03 12:33:42
Hi following Programming with dplyr I noticed that one can add a name using quo_name. I was wondering how to do this for multiple columns, eg. like a quos_name of sorts. E.g.: my_mutate <- function(df, expr) { expr <- enquo(expr) mean_name <- paste0("mean_", quo_name(expr)) sum_name <- paste0("sum_", quo_name(expr)) mutate(df, !!mean_name := mean(!!expr), !!sum_name := sum(!!expr) ) } becomes my_mutate <- function(df, ...) { exprs <-quos(...) mean_names <- paste0("mean_", quos_name(exprs)) sum_names <- paste0("sum_", quos_name(exprs)) mutate(df, !!!mean_names := mean(!!!exprs), !!!sum_names :=

use dplyr mutate() in programming

冷暖自知 提交于 2019-12-03 07:57:01
I am trying to assign a column name to a variable using mutate. df <-data.frame(x = sample(1:100, 50), y = rnorm(50)) new <- function(name){ df%>%mutate(name = ifelse(x <50, "small", "big")) } When I run new(name = "newVar") it doesn't work. I know mutate_() could help but I'm struggling in using it together with ifelse . Any help would be appreciated. Using dplyr 0.7.1 and its advances in NSE, you have to UQ the argument to mutate and then use := when assigning. There is lots of info on programming with dplyr and NSE here: https://cran.r-project.org/web/packages/dplyr/vignettes/programming

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

一世执手 提交于 2019-12-03 03:04:31
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 expected because species is interpreted in the context of the iris data frame instead of being interpreted in

Dynamic select expression in function [duplicate]

我的未来我决定 提交于 2019-12-02 06:54:57
This question already has an answer here: Reshaping multiple sets of measurement columns (wide format) into single columns (long format) 7 answers I am trying to write a function that will convert this data frame library(dplyr) library(rlang) library(purrr) df <- data.frame(obj=c(1,1,2,2,3,3,3,4,4,4), S1=rep(c("a","b"),length.out=10),PR1=rep(c(3,7),length.out=10), S2=rep(c("c","d"),length.out=10),PR2=rep(c(7,3),length.out=10)) obj S1 PR1 S2 PR2 1 1 a 3 c 7 2 1 b 7 d 3 3 2 a 3 c 7 4 2 b 7 d 3 5 3 a 3 c 7 6 3 b 7 d 3 7 3 a 3 c 7 8 4 b 7 d 3 9 4 a 3 c 7 10 4 b 7 d 3 In to this data frame df %>%

Dynamic select expression in function [duplicate]

偶尔善良 提交于 2019-12-02 06:40:57
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed last year . I am trying to write a function that will convert this data frame library(dplyr) library(rlang) library(purrr) df <- data.frame(obj=c(1,1,2,2,3,3,3,4,4,4), S1=rep(c("a","b"),length.out=10),PR1=rep(c(3,7),length.out=10), S2=rep(c("c","d"),length.out=10),PR2=rep(c(7,3),length.out=10)) obj S1 PR1 S2 PR2 1 1 a 3 c 7 2 1 b 7 d 3 3 2

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