dplyr Use Cases: Non-Interactive Mode

[This article was first published on Just Another Data Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The current release of dplyr (v 0.4.1) offers lot more flexibility regarding usage of important verbs in non-interactive mode. In this post, I’m exploring different possible use-cases.


  • group_by_, select_, rename_:
For group_by_, select_ and rename_, we can pass a character vector of variable names as an argument to .dots parameter.


  • filter_:
To use filter_ function, we need to pass filter criteria as a parameter to .dots. The criteria can be created using lazyeval::interp function.


  • mutate_, transmute_, summarise_:
We need to provide 2 things to these functions – a list of functions to be applied on the input variables (with corresponding input variables) and a character vector of output variables names. These 2 things can be passed to the .dots argument using combination of lazyeval::interp  and setNames function.

  • joins:
For 2 table verbs, there’s no *_join_ function and we don’t need one for general purposes. We can just pass a named vector to by argument. setNames function comes in handy while doing this.


The R Code for the above mentioned use cases is shown below and can also be found on this GitHub Gist.


# using dplyr finctions in non-interactive mode
# examples
library(plyr)
library(dplyr)
d1 = data_frame(x = seq(1,20),y = rep(1:10,2),z = rep(1:5,4))
head(d1)
#### single table verbs ####
# group_by
group_by_fn <- function(d_in,gp_vec){
d_out = d_in %>%
group_by_(.dots = gp_vec)
}
gp_vec = c("y","z")
d1_gp_by_out = group_by_fn(d1,gp_vec)
head(d1_gp_by_out)
# select/rename (haven't included drop variables case)
select_fn <- function(d_in,sel_vec){
d_out = d_in %>%
select_(.dots = sel_vec)
}
sel_vec = c("x","y")
d1_select_out = select_fn(d1,sel_vec)
head(d1_select_out)
# filter
filter_fn <- function(d_in,filter_crit){
d_out = d_in %>%
filter_(filter_crit)
}
y_vec = 6:8
filter_crit = interp(~ filter_var %in% y_vec,filter_var = as.name("y"))
d1_filter_out = filter_fn(d1,filter_crit)
head(d1_filter_out)
z_vec = 1:2
filter_crit2 = interp(~ filter_var1 %in% y_vec & filter_var2 %in% z_vec,.values = list(filter_var1 = as.name("y"),
filter_var2 = as.name("z")))
d1_filter2_out = filter_fn(d1,filter_crit2)
head(d1_filter2_out)
# mutate, transmute, summarise
mutate_fn <- function(d_in,op_ls,var_vec){
d_out = d_in %>%
mutate_(.dots = setNames(op_ls,var_vec))
}
var1_rng = 3:5
op_ls = list(interp(~f(var1,var2), .values = list(f = as.name("*"),
var1 = as.name("x"),
var2 = as.name("y"))),
interp(~f(var1 %in% var1_rng,var2,var3),.values= list(f = as.name("ifelse"),
var1 = as.name("x"),
var2 = as.name("y"),
var3 = as.name("z"))))
var_vec = c("yy","zz")
d1_mutate_out = mutate_fn(d1, op_ls, var_vec)
head(d1_mutate_out)
var_ls = list("yy","zz")
d1_mutate_out1 = mutate_fn(d1, op_ls, var_ls)
head(d1_mutate_out1)
#### two table verbs ####
# joins
d2 = data_frame(xx = seq(1,20),yy = rep(1:10,2),zz = rep(1:2,10))
join_fn <-function(d_in1,d_in2,var_vec1,var_vec2){
d_out = d_in1 %>%
left_join(d_in2,setNames(var_vec2,var_vec1))
}
var_vec1 = c("x","y")
var_vec2 = c("xx","yy")
d_join_out = join_fn(d1,d2,var_vec1,var_vec2)
head(d_join_out)
# everything combined (essentially, power of %>%)
d_combined_out = d1 %>%
filter_fn(filter_crit) %>%
group_by_fn(gp_vec) %>%
mutate_fn(op_ls,var_vec) %>%
select_fn(c("x","y","z")) %>%
join_fn(.,d2,var_vec1,var_vec2)
head(d_combined_out)
# sources:
# http://cran.r-project.org/web/packages/dplyr/vignettes/nse.html
# http://stackoverflow.com/questions/28125816/r-standard-evalation-for-join-dplyr

To leave a comment for the author, please follow the link and comment on their blog: Just Another Data Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)