Reputation: 73
What does the .$avg signify?
conditional_avg <- galton_heights %>%
filter(round(father) == 72) %>%
summarize(avg = mean(son)) %>%
.$avg
Upvotes: 0
Views: 632
Reputation: 887951
The .$
is used to extract the column avg
as a vector
. Here, .
represents the data coming from the lhs
of the %>%
. It can be vector
or list
or data.frame
. In this case, it is a data.frame
with a single column 'avg'. We use $
or [[
to extract the column as a vector
. There is also a convenient function pull
to do this
library(tidyverse)
galton_heights %>%
filter(round(father) == 72) %>%
summarize(avg = mean(son)) %>%
pull(avg)
As a reproducible example, using data(mtcars)
, if we don't extract the 'avg' column, it will be a data.frame
with a single column
mtcars %>%
summarise(avg = mean(hp)) %>%
str
# 'data.frame': 1 obs. of 1 variable:
#$ avg: num 147
Extracting the column returns as a vector
.
mtcars %>%
summarise(avg = mean(hp)) %>%
.$avg
#[1] 146.6875
mtcars %>%
summarise(avg = mean(hp)) %>%
pull
#[1] 146.6875
Upvotes: 3