After reading and re-reading many of the dplyr programming tutorials, I still can't find a way to solve my specific case.
I understand that the use of group_by_ , mutate_ and such "string-friendly" versions of tidyverse functions goes out of date and that enquo is the way to go.
However, my case is somewhat different, and I try my best to find a neat way to solve it neatly.
In fact, my goal is to create and manipulate data within a function. Creation (change) of new variables based on others, their use, etc.
However, no matter how hard I try, my code either errors or returns some warnings when checking packages, for example no visible binding for global variable ...
Here's a reproducible example:
Here is what I want to do:
df <- data.frame(X=c("A", "B", "C", "D", "E"), Y=c(1, 2, 3, 1, 1)) new_df <- df %>% group_by(Y) %>% summarise(N=n()) %>% mutate(Y=factor(Y, levels=1:5)) %>% complete(Y, fill=list(N = 0)) %>% arrange(Y) %>% rename(newY=Y) %>% mutate(Y=as.integer(newY))
Some common dplyr manipulations whose expected result should be:
# A tibble: 5 x 3 newY NY <fctr> <dbl> <int> 1 1 3 1 2 2 1 2 3 3 1 3 4 4 0 4 5 5 0 5
I would like this piece of code to work quietly inside a function. Following was my best attempt at resolving non-NSE issues:
myfunction <- function(){ df <- data.frame(X=c("A", "B", "C", "D", "E"), Y=c(1, 2, 3, 1, 1)) new_df <- df %>% group_by_("Y") %>% summarise(!!"N":=n()) %>% mutate(!!"Y":=factor(Y, levels=1:5)) %>% complete_("Y", fill=list(N = 0)) %>% arrange_("Y") %>% rename(!!"newY":="Y") %>% mutate(!!"Y":=as.integer(newY)) }
Unfortunately, I still have the following posts:
myfunction: no visible global function definition for ':=' myfunction: no visible binding for global variable 'Y' myfunction: no visible binding for global variable 'newY' Undefined global functions or variables: := Y n.Factors n_optimal newY
Is there any way to solve this problem? Thank you very much!
EDIT: I am using R 3.4.1, dplyr_0.7.4, tidyr_0.7.2 and tidyverse_1.1.1
ANSWER
Thanks to the comments I managed to solve, here is a working solution:
myfunction <- function(){ df <- data.frame(X=c("A", "B", "C", "D", "E"), Y=c(1, 2, 3, 1, 1)) new_df <- df %>% group_by_("Y") %>% summarise_("N"=~n()) %>% mutate_("Y"= ~factor(Y, levels=1:5)) %>% complete_("Y", fill=list(N = 0)) %>% arrange_("Y") %>% rename_("newY"=~Y) %>% mutate_("Y"=~as.integer(newY)) }
Thanks LOT :)