Display name
Display name

Reputation: 4481

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

df1 <- data.frame(
  " " = c(" ", "Part Number 1", "Part Number 2", "Part Number 3"), 
  Julie = c("Measurement 1", 33, 34, 33),
  Julie = c("Measurement 2", 32, 31, 31),
  Joe = c("Measurement 1", 33, 33, 30),
  Joe = c("Measurement 2", 31, 32, 31))

df1 %>%
  mutate_all(as.character) %>% 
  set_names(c("Part", paste(names(.)[2:ncol(.)], .[1, 2:ncol(.)], sep = "-"))) %>%
  `[`(2:nrow(.), ) %>%
  gather("key", "value", contains("Measurement")) %>%
  separate("key", c("person", "measurement"), sep = "-") %>%
  mutate_at("person", ~ stringr::str_replace(.x, "\\..*",""))   # line 14
  # mutate_at("person", ~ stringr::str_replace(., "\\..*",""))  # line 15

There's only one thing I want to highlight in the code above, and that's the difference between line #14 and line #15. Notice that the str_replace() piped data set for line #14 is .x and the piped data set for line #15 is just .. I'm used to seeing things in the form of line #15, and it seems the x in line #14 is benign.

But the x is not benign. If I include it in similar mtcars code (below) I get an error instead of a 'TRUE'. Can you explain this? I've never seen this mysterious x syntax before, which hardly means it's not important (or common).

identical(mtcars %>% .[1:2, ],
          mtcars %>% .x[1:2, ])
# Error in `[.data.frame`(., .x, 1:2, ) : object '.x' not found

Upvotes: 5

Views: 2426

Answers (1)

MrFlick
MrFlick

Reputation: 206197

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.

Upvotes: 10

Related Questions