Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr: sum inside consecutive mutate

Tags:

r

dplyr

sum

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?

like image 630
Habert Avatar asked Jun 16 '17 19:06

Habert


People also ask

How do I sum across rows in R dplyr?

Syntax: mutate(new-col-name = rowSums(.)) The rowSums() method is used to calculate the sum of each row and then append the value at the end of each row under the new column name specified. The argument . is used to apply the function over all the cells of the data frame. Syntax: rowSums(.)

How do you calculate row wise sum in R?

To find the row wise sum of n number of columns can be found by using the rowSums function along with subsetting of the columns with single square brackets.

How do I sum two rows in R?

First of all, create a data frame. Then, using plus sign (+) to add two rows and store the addition in one of the rows. After that, remove the row that is not required by subsetting with single square brackets.

How do I sum multiple columns in R?

We can calculate the sum of multiple columns by using rowSums() and c() Function. we simply have to pass the name of the columns.


1 Answers

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.

like image 126
m0nhawk Avatar answered Oct 23 '22 20:10

m0nhawk