One of the advantages of using the pipe %>%
is that we do not have to
keep naming new objects as we manipulate the data frame. As a quick
reminder, if we want to compute the median murder rate for states in the
southern states, instead of typing:
tab_1 <- filter(murders, region == "South")
tab_2 <- mutate(tab_1, rate = total / population * 10^5)
rates <- tab_2$rate
median(rates)
#> [1] 3.4
We can avoid defining any new intermediate objects by instead typing:
filter(murders, region == "South") %>%
mutate(rate = total / population * 10^5) %>%
summarize(median = median(rate)) %>%
pull(median)
#> [1] 3.4
We can do this because each of these functions takes a data frame as the
first argument. But what if we want to access a component of the data
frame. For example, what if the pull
function was not available and we
wanted to access tab_2$rate
? What data frame name would we use? The
answer is the dot operator.
For example to access the rate vector without the pull
function we
could use
rates <- filter(murders, region == "South") %>%
mutate(rate = total / population * 10^5) %>%
.$rate
median(rates)
#> [1] 3.4
In the next section, we will see other instances in which using the .
is useful.