referring to variables by name in selecting functions

This understanding is incorrect. . refers to the object on the LHS of the "current stage" of the pipe. It does not refer to the original data set at the start of the pipeline. You can verify this with the following code.

library(dplyr, warn.conflicts = FALSE)
#> Warning: package 'dplyr' was built under R version 4.0.4

# If . referred to the original data.frame, this code should work. But it does not.
iris %>% 
  select(-Species) %>% 
  rename(Species_new = .[[Species]])
#> Error: object 'Species' not found

Created on 2021-03-21 by the reprex package (v1.0.0)

.data is a pronoun; a special construct which is used to disambiguate between data variables and environment variables. See this post for a detailed write-up on this topic.

2 Likes