I\'m trying to develop a deeper understanding of using the dot (".") with dplyr and using the .data pronoun with dplyr. The code
Compare mtcars %>% count(.data[["cyl"]]) vs. mtcars %>% count(.[["cyl"]]).
mtcars %>% count(.[["cyl"]])
.[["cyl"]] n
1 4 11
2 6 7
3 8 14
mtcars %>% count(.data[["cyl"]])
cyl n
1 4 11
2 6 7
3 8 14
. is literally just the previous result. So the first is similar to:
. <- mtcars
count(., .[["cyl"]])
The second is a shorthand for looking up the variable by the string "cyl" and treating the previous result as the search path for the variable. For example, suppose you mispelled your variable name:
mtcars %>% count(.[["cyll"]])
n
1 32
mtcars %>% count(.data[["cyll"]])
Error: Must group by variables found in `.data`.
* Column `cyll` is not found.
Using . will not throw an error because indexing to a non-existing column is a valid base-R operation that returns NULL.
Using .data will throw because using a non-existent variable:
mtcars %>% count(cyll)
Also throws.