dplyr piping data – difference between `.` and `.x`

The . is the basic unit of transfer for the magrittr pipelines (which dplyr imports). It contains the value coming from the pipe.

The .x value is something that the tidyverse world added. It’s used then you have anonymous functions created with the ~ (tilde) syntax. This calls rlang::as_function to turn that formula into a function. It’s basically a short cut so rather than having to type out function(x) x+5, you can just write ~.x+5. Since functions can have more than one parameter, it can be helpful to use names for that parameter so .x refers to the first parameter (and .y the second). The as_function also allows you to use . as an alias for the first parameter. It can do this because the ~ creates a formula and magrittr doesn’t generally replaces . in formulas so the mapper is free to re-interpret the .. You can see the function signature here

f <- rlang::as_function(~.x+5)
f
# <lambda>
# function (..., .x = ..1, .y = ..2, . = ..1) 
# .x + 5
# attr(,"class")
# [1] "rlang_lambda_function"

You can see how both . and .x are alias for ..1 which is the first parameter passed to the function.

Leave a Comment