Motivating scenario: you want to change or add a column in a tibble.
Learning goals: By the end of this sub-chapter you should be able to
Add or change a column with the mutate() function in the dplyr package.
Change between variable types in a column.
Change values conditionally with the case_when() function.
Figure 1: An illustration of the mutate() function. The top table represents the original dataset, containing columns for the proportion of hybrids (prop_hyb) and the number of individuals assayed (n_assayed). The mutate() function is then applied to compute n_hyb, the total number of hybrid individuals, by multiplying prop_hyb by n_assayed. The resulting dataset, shown in the bottom table, includes this newly created n_hyb column.
Often we want to change the values in a column, or make a new column. For example in our data we may hope to:
Convert growth_rate into a number, with the as.numeric() function.
Add the logical variable, visited, which is TRUE if a plant had more than zero pollinators visit them, and is FALSE otherwise.
The mutate() function in the dplyr package can solve this. You can overwrite data in an existing column or make a new column as follows:
ril_data |> dplyr::mutate(growth_rate =as.numeric(growth_rate), # make numericvisited = mean_visits >0)
Warning: There was 1 warning in `dplyr::mutate()`.
ℹ In argument: `growth_rate = as.numeric(growth_rate)`.
Caused by warning:
! NAs introduced by coercion
You can see that R gave us a warning. Warnings do not mean that something necessarily went wrong, but they do mean we should look and see what happened. In this case, we see that when trying to change the character string, 1.8O, into a number R did not know what to do and converted it to NA. In the next bit of code I convert it into "1.80" with the case_when() function.
After confirming this worked, we can assign it to R’s memory: In doing so, I even converted 1.8O into 1.80 so we have an observation in that cell rather than missing data.
Be careful combining classes with case_when()Click the arrow to learn more
When I was trying to change the character “1.8O” into the 1.80, R kept saying: Error in dplyr::mutate()… Caused by error in case_when(): ! Can’t combine ..1 (right) and ..2 (right) . Unlike warnings, which tell you to watch out, errors tell you R cannot do what you’re asking of it. It turns out that I could not assign the number 1.80 to the vector held in petal_area_mm because I could not blend characters add numbers. So, as you can see, I replaced "1.8O" with "1.80", and then I used as.numeric() to convert the vector to numeric.