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?