The mutate() function from dplyr is extremely useful one for adding new columns to a dataframe based on computations from existing columns. It is a vectorized function, though, and is often misunderstood as working row-wise when it actually works column-wise, that is, on whole vectors with R's built-in recycling. This behavior can often be confusing for those looking to use mutate() on non-trivial examples or with custom functions, so, in this recipe, we're going to examine how mutate() actually behaves in certain situations, with the hope that this will be enlightening.
Writing functions for use in dplyr::mutate()
Getting ready
For this, we'll need the dplyr package and the built-in iris data.
...