We want all the necessary information for our analysis to be included in the data table. So the first task is to add the murder rates to our murders data frame. The function mutate
takes the data frame as a first argument and the name and values of the variable as a second argument using the convention name = values
. So, to add murder rates, we use:
library(dslabs)
data("murders")
murders <- mutate(murders, rate = total / population * 100000)
We can see that the new column is added:
head(murders)
#> state abb region population total rate
#> 1 Alabama AL South 4779736 135 2.82
#> 2 Alaska AK West 710231 19 2.68
#> 3 Arizona AZ West 6392017 232 3.63
#> 4 Arkansas AR South 2915918 93 3.19
#> 5 California CA West 37253956 1257 3.37
#> 6 Colorado CO West 5029196 65 1.29
Run the sample code to see how mutate() function works.
library(dplyr)
library(dslabs)
data("murders")
# Add the rate column
murders <- mutate(murders, rate = total / population * 100000)
# Check murders to find that the new column is added.
head(murders)