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

查看:50
本文介绍了dplyr管道数据-`.`和`.x`之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  df1<-data.frame(" = c(",部件号1",部件号2",部件号3"),朱莉= c("Measurement 1",33,34,33),朱莉= c("Measurement 2",32,31,31),乔= c(度量1",33,33,30),乔= c(测量2",31、32、31))df1%>%mutate_all(as.character)%&%set_names(c("Part",paste(names(.)[2:ncol(.)],.[1,2:ncol(.)],sep =-")))%>%`[`(2:nrow(.),)%>%collect("key","value",contains("Measurement"))%&%;%split("key",c("person","measurement"),sep =-")%&%mutate_at("person",〜stringr :: str_replace(.x,"\\ .. *","))#第14行#mutate_at("person",〜stringr :: str_replace(.,"\\ .. *","))#第15行 

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

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

  identical(mtcars%>%.[1:2,],mtcars%>%.x [1:2,])#`[.data.frame`(.,.x,1:2,)中的错误:找不到对象'.x' 

解决方案

. magrittr 管道( dplyr 导入).它包含来自管道的值.

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

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

您可以看到. .x 都是 .. 1 的别名,这是传递给函数的第一个参数./p>

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

解决方案

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.

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

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