I need to bind two data.frames using a user-defined function. As example let's imagine that the data frames look like this.
library(dplyr)
library(lazyeval)
df<-data.frame(type1=c("a","b","c","a","b","c",NA),type2=c("d","e","f","d","e","f","f"))
f<-function(x){
y<-df%>%
dplyr::filter_(lazyeval::interp(~!is.na(x),x=as.name(x)))%>%
dplyr::group_by_(x)%>%
dplyr::summarize("Sum"=sum(type2=="d"))
y<-dplyr::bind_rows(y,data.frame(x="Total",Sum=sum(y$Sum)))
return(y)
}
result_f<-f("type1")
The problem is that this function assumes that the name of variable "Total" in the second data frame is "x" instead of "Total" creating an additional column due to the mismatch with the first data frame.
How can the function interpret x as a variable instead of a string? Unquoting? How?