Difference between dplyr:mutate and transform when using pmin and pmax?

dplyr, r

Solution

It appears my suspicions are correct, and that it is by design to allow the use of computed variables immediately afterwards, eg:

data.frame(a=1:4,b=5:8) %>% mutate(sum=a+b, letter=letters[sum])
  a b sum letter
1 1 5   6      f
2 2 6   8      h
3 3 7  10      j
4 4 8  12      l

In order to replicate the expected behaviour from `transform` one needs to simply reference the variable directly:

x %>% mutate(a=pmin(x$a,x$b), b=pmax(x$a,x$b))
   a  b
1  0  1
2  0  2
3  0  3
4  0  4
5  0  5
6  0  6
7  0  7
8  0  8
9  0  9
10 0 10
11 0  1
12 1 11
13 1 12
14 1 13
15 1 14
16 1 15
17 1 16
18 1 17
19 1 18
20 1 19
21 0  3

Problem

While trying to answer this question, I encountered a difference between `mutate` and `transform` in what I expected to be equivalent operations. ``` # data x <- data.frame(a=c(rep(0,10),rep(1,10),3),b=c(1:10,0,11:19,0)) #transform transform(x,a=pmin(a,b), b=pmax(a,b)) a b 1 0 1 2 0 2 3 0 3 4 0 4 5 0 5 6 0 6 7 0 7 8 0 8 9 0 9 10 0 10 11 0 1 12 1 11 13 1 12 14 1 13 15 1 14 16 1 15 17 1 16 18 1 17 19 1 18 20 1 19 21 0 3 #mutate libarary(dplyr) x %>% mutate(a=pmin(a,b), b=pmax(a,b)) a b 1 0 1 2 0 2 3 0 3 4 0 4 5 0 5 6 0 6 7 0 7 8 0 8 9 0 9 10 0 10 11 0 0 12 1 11 13 1 12 14 1 13 15 1 14 16 1 15 17 1 16 18 1 17 19 1 18 20 1 19 21 0 0 ``` Note the differences in lines 11 and 21. I suspect that `mutate` is mutating the data as it goes and therefore, `pmax` is not seeing the original data. Is this correct? Is it a bug, or by design?

Original source