In many cases, after grouping a data frame by some variables, I want to apply a function that uses data from another data frame, grouped by the same variables. The best solution I found is to use the sem_join function inside the function as follows:
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))
}
d1 %>% group_by(model) %>% do(myfun(., d2))
The problem is that sem_join returns “Merge ...” messages, and since I use the function to load, I get a lot of messages that reset the console. So, is there a way to reduce the verbosity of joins? Do you know a more elegant way to do something like this?
PS A few years ago I asked a similar question for plyr: a subset inside a function using the variables specified in ddply