dplyr::mutate()works the same way asplyr::mutate()and similarly tobase::transform(). The key difference betweenmutate()andtransform()is thatmutateallows you to refer to columns that you just created. - Introduction to dplyr
There are some differences between the mutate function in dplyr and plyr. The main difference is of course that plyr::mutate can be applied to lists and dplyr::mutate is faster.
Moreover, when referring to the just created columns, plyr cannot reassign them again, but dplyr does.
# creating a temporary variable and removing it later
plyr::mutate(data.frame(a = 2), tmp = a, c = a*tmp, tmp = NULL)
## a tmp c
## 1 2 2 4
dplyr::mutate(data.frame(a = 2), tmp = a, c = a*tmp, tmp = NULL)
## a c
## 1 2 4
# creating a temporery variable and changing it later
plyr::mutate(data.frame(a = 2), b = a, c = a*b, b = 1)
## a b c
## 1 2 2 4
dplyr::mutate(data.frame(a = 2), b = a, c = a*b, b = 1)
## a b c
## 1 2 1 4
Now I am looking for the functionality of the dplyr mutate function for list objects. So I am looking for a function that mutates a list and can reassign just created variables.
plyr::mutate(list(a = 2), b = a, c = a*b, b = 1)
## $a
## [1] 2
##
## $b
## [1] 2
##
## $c
## [1] 4
dplyr::mutate(list(a = 2), b = a, c = a*b, b = 1)
## Error in UseMethod("mutate_") :
## no applicable method for 'mutate_' applied to an object of class "list"
desired_mutate(list(a = 2), b = a, c = a*b, b = 1)
## $a
## [1] 2
##
## $b
## [1] 1
##
## $c
## [1] 4
I realize that in this simple case, I can just use
plyr::mutate(list(a = 2), c = {b = a; a*b})
But in my actual use case, I assign random numbers to a temporary variable and would like to remove it afterwards. Something like the following:
desired_mutate(list(a = c(1, 2, 5, 2)),
tmp = runif(length(a)),
b = tmp * a,
c = tmp + a,
tmp = NULL)