jrnold / r4ds-exercise-solutions

Exercise solutions to "R for Data Science"
https://jrnold.github.io/r4ds-exercise-solutions
Creative Commons Attribution 4.0 International
320 stars 229 forks source link

use `.data$` instead of `.$` for Exercise 24.3.3 & Exercise 24.3.4 #169

Closed dongzhuoer closed 5 years ago

dongzhuoer commented 5 years ago

I think that the biggest drawback for dplyr is using system function name as variable name.

For example,

tibble::tribble(
    ~date, ~mean,
    '2018-11-22', 1
)

would cause great trouble for dplyr::mutate() and so on.

dongzhuoer commented 5 years ago

date and wday are both function name, so I understand that using .$ can reduce ambiguity.

But there is another problem, x %>% f(g(.)) means x %>% f(., g(.))

From ?`%>%`

iris %>% subset(1:nrow(.) %% 2 == 0) is equivalent to iris %>% subset(., 1:nrow(.) %% 2 == 0)

dongzhuoer commented 5 years ago

So every time I use . in nested function, I would add {} to create a lambda expression.

dongzhuoer commented 5 years ago

what about .data$ since they are all inside dplyr::mutate()

dongzhuoer commented 5 years ago

I checked the book, it neither introduces .$ not .data$.

We may need to add a paragraph explaining why we use .data$.

jrnold commented 5 years ago

At one point .$ was required to get case_when() to work within mutate(). This was before tidyeval standardized everything. I'll need to check what will work now.

jrnold commented 5 years ago

Fixed in 159facabaafc697b2