Habert
Habert

Reputation: 347

dplyr: sum inside consecutive mutate

library(dplyr)
tib <- tibble(a = c(1,2,3))

The following work as expected:

tib %>% mutate(b = a^2, c = sqrt(b))
# A tibble: 3 x 3
      a     b     c
  <dbl> <dbl> <dbl>
1     1     1     1
2     2     4     2
3     3     9     3

tib %>% mutate(b = a^2, c = sum(a))
# A tibble: 3 x 3
      a     b     c
  <dbl> <dbl> <dbl>
1     1     1     6
2     2     4     6
3     3     9     6

tib %>% mutate(b = a^2) %>% mutate(c = sum(b))
# A tibble: 3 x 3
      a     b     c
  <dbl> <dbl> <dbl>
1     1     1    14
2     2     4    14
3     3     9    14

The following does not:

tib %>% mutate(b = a^2, c = sum(b))
# A tibble: 3 x 3
      a     b             c
  <dbl> <dbl>         <dbl>
1     1     1 1.482197e-323
2     2     4 1.482197e-323
3     3     9 1.482197e-323

I would expect the result in column c to be the same as above, 14 everywhere. Any insight about what I am doing wrong?

Upvotes: 5

Views: 231

Answers (1)

m0nhawk
m0nhawk

Reputation: 24168

I have checked with both dplyr versions: it's looks like a bug in new tidyeval engine. I have filed the bug on Github.


Update:

This is now fixed. Issue. The new version of dplyr 0.7.1 and above doesn't have this issue anymore.

Upvotes: 4

Related Questions