0

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?

ungatoverde
  • 161
  • 12
  • 2
    It would be nice if you made your example [reproduicble](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including sample input data and the desired output so possible solutions can be tested and verified. – MrFlick May 10 '17 at 18:33
  • Sorry, I wrongly pasted the script. I just add it to the question. – ungatoverde May 10 '17 at 18:45

1 Answers1

1

You can change the last line in the function to

y <- dplyr::bind_rows(y,setNames(data.frame("Total",sum(y$Sum)), c(x, "Sum")))

That will set the names of the data.frame you are trying to bind in to the original names.

Before you spend too much time learning all the underscore functions in dplyr, note that in the next version (0.6) they are being superseded by a completely different method of non-standard evaluation. Read more here: https://blog.rstudio.org/2017/04/13/dplyr-0-6-0-coming-soon/

MrFlick
  • 195,160
  • 17
  • 277
  • 295