Skip to content
Prev 371290 / 398530 Next

Avoid duplication in dplyr::summarise

Hi Lars,
Two comments:
1. You can achieve what you want with a slight modification of your
definition of s(), using the hint from the error message that you need an
argument '.':
s <- function(.) {
  dplyr::summarise(., x1m = mean(X1),
                   x2m = mean(X2),
                   x3m = mean(X3),
                   x4m = mean(X4))
}

2. You have not given a great test case in how you set your two factors
because the two group_by()'s will give the identical groupings, An
alternative which confirms that the function s() does what you want might
be:

df <- data.frame(matrix(rnorm(40), 10, 4),
                 f1 = base::sample(letters[1:3],30,replace=TRUE),
                 f2 = base::sample(letters[4:6],30,replace=TRUE))

HTH,

Eric
On Sat, Sep 9, 2017 at 1:52 PM, Edjabou Vincent <maklawe at gmail.com> wrote: