Question: In many occasions, after grouping a data frame by some variables, I want to apply a function that uses data from another data frame that is grouped by the same variables. The best solution I found is to use *semi\_join* inside the function as follow:

```
d1 <- data.frame(model = c(1,1,2,2), x = runif(4) )
d2 <- data.frame(model=c(1,1,1,2,2,2), y = runif(6) )

myfun <- function(df1, df2) {
   subsetdf2 <- semi_join(df2, df1)
   data.frame(z = sum(d1$x) - sum(subsetdf2$y)) # trivial manipulation just to exemplify
}

d1 %>% group_by(model) %>% do(myfun(., d2))

```

The problem is that *semi\_join* returns 'Joining by...' messages and, as I am using the function to do bootstrap, I get many messages that collapse the console. So, is there any way to reduce the verbosity of joins? Do you know a more elegant way to do something like this?

P.S. I asked a similar question a few years ago for *plyr*: [subset inside a function by the variables specified in ddply](https://stackoverflow.com/questions/20171053/subset-inside-a-function-by-the-variables-specified-in-ddply)

Answer: 
If all you want to do is stop the 'Joining by: ' statement, you just need to specify what column you are joining on with the `by` argument.

For example:

```
semi_join(d2, d1, by="model")

```

EDIT - As an alternative to using `semi_join` you can use a `base` solution. As the `group_by` function is passing the data by groups, you can filter using a simple indexing statement. This will avoid the need for an additional parameter. **This also currently assumes that the column of interest is the first column.**

```
myfun <- function(df1, df2) {
  subsetdf2 <- df2[df2[,1] %in% unique(df1[,1]),]
  data.frame(z = sum(df1$x) - sum(subsetdf2$y)) # trivial manipulation just to exemplify
}

```
I adapted the solution of @cdeterman. It is a bit redundant though. 

```
d1 <- data.frame(model = c(1,1,2,2), x = runif(4) )
d2 <- data.frame(model=c(1,1,1,2,2,2), y = runif(6) )

myfun <- function(df1, df2, gv) {
  subsetdf2 <- semi_join(df2, df1, by = gv)
  data.frame(z = sum(d1$x) - sum(subsetdf2$y)) # trivial manipulation just to     exemplify
}

group_var <- 'model'
d1 %>% group_by_(group_var) %>% do(myfun(., d2,group_var))

```