dplyr 管道数据 - `.` 和 `.x` 之间的区别 [英] dplyr piping data - difference between `.` and `.x`

查看:14
本文介绍了dplyr 管道数据 - `.` 和 `.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

在上面的代码中,我只想强调一件事,那就是第 14 行和第 15 行之间的区别.请注意,第 14 行的 str_replace() 管道数据集是 .x,第 15 行的管道数据集只是 ..我习惯于以第 15 行的形式查看事物,而且第 14 行中的 x 似乎是良性的.

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.

但是 x 不是良性的.如果我将它包含在类似的 mtcars 代码(如下)中,我会收到错误而不是TRUE".你能解释一下吗?我以前从未见过这种神秘的 x 语法,这并不意味着它不重要(或不常见).

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

推荐答案

.magrittr 管道(其中 dplyr 进口).它包含来自管道的值.

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

.x 值是 tidyverse 世界添加的值.使用它之后,您就可以使用 ~(波浪号)语法创建匿名函数.这将调用 rlang::as_function 将该公式转换为函数.这基本上是一个捷径,因此您不必键入function(x) x+5,而只需编写~.x+5.由于函数可以有多个参数,因此使用该参数的名称会很有帮助,因此 .x 指的是第一个参数(而 .y 指的是第二个).as_function 还允许您使用 . 作为第一个参数的别名.它可以这样做是因为 ~ 创建了一个公式,而 magrittr 通常不会替换公式中的 .,因此映射器可以自由地重新解释..你可以在这里看到函数签名

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"

您可以看到 ..x 如何成为 ..1 的别名,..1 是传递给函数的第一个参数.

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

这篇关于dplyr 管道数据 - `.` 和 `.x` 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆