在 R 中使用波浪号 (~) 和句号 (.) [英] Use of Tilde (~) and period (.) in R

查看:116
本文介绍了在 R 中使用波浪号 (~) 和句号 (.)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Hadley 的 R4DS 书使用 tidyverse 和 purrr 进行循环,但对波浪号 ~ 符号和句点符号的确切用法有点困惑.

I'm going over looping with tidyverse and purrr using Hadley's R4DS book and am a little confused as to the exact usage of the tilde ~ symbol and period symbol.

因此,在编写 for 循环或使用 map() 时,您似乎可以使用波浪号代替 ~.

So when writing for loops, or using map(), instead of writing out function(), it appears you can use the tilde symbol instead ~.

这是否仅适用于 for 循环?

Does this only apply to for loops?

如下...

models <- mtcars %>% 
  split(.$cyl) %>% 
  map(~lm(mpg ~ wt, data = .))

此外,我被告知的句点可用于引用当前列表元素".但我很困惑这意味着什么.这是否意味着,只有在循环时,句点才表示它指的是正在循环的列表中的元素?它与管道有什么不同?当您使用管道时,您正在将一行的结果通过管道传输到下一行代码.

Additionally, the period i was told can be used "to refer to the current list element". But I am confused what that means. Does that mean, that only when looping, the period means it refers to the element in the list that is being looped over? How is it different from piping? When you pipe, you are piping the result of one line to the next line of code.

所以在上面的例子中,mtcars 通过 split() 被传送到第二行,但使用了句点.为什么?

So in the case above, mtcars is piped to the second line with split() but a period is used. Why?

下面的案例总结了我的困惑:

The case below sums up my confusion:

x <- c(1:10)

detect(x, ~.x > 5)

使用检测功能,找到第一个匹配项,我以为我可以使用

using the detect function, which finds the first match, I thought i could just use

detect(x, x >5)

但我收到一个错误消息,说 x > 5 不是函数.所以我加了一个波浪号

but I get an error saying x >5 is not a function. So i add a tilde

detect(x, ~ x > 5)

并得到一个错误,说它需要一个 TRUE 或 FALSE,而不是 10.所以如果你添加一个句点

and get an error sayingt it expects a single TRUE or FALSE, not 10. So if you add a period

detect(x, ~.x >5) 

突然它可以作为循环工作.那么 ~ 和 .在这里,如何.与简单的管道相比?

suddenly it works as looping. So what is the relation/ usage of ~ and . here and how does . compare to simple piping?

推荐答案

这个整体被称为 tidyverse 非标准评估 (NSE).您可能发现 ~在公式中使用表示左侧是依赖于右手边.

This overall is known as tidyverse non-standard evaluation (NSE). You probably found out that ~ also is used in formulas to indicate that the left hand side is dependent on the right hand side.

tidyverse NSE中,~表示function(...).因此,这两个表达式是等价的.

In tidyverse NSE, ~ indicates function(...). Thus, these two expressions are equivalent.

x %>% detect(function(...) ..1 > 5)
#[1] 6

x %>% detect(~.x > 5)
#[1] 6

~ 自动将函数的每个参数分配给 .;.x, .y;和 ..1, ..2 ..3 特殊符号.注意只有第一个参数变成了 ..

~ automatically assigns each argument of the function to the .; .x, .y; and ..1, ..2 ..3 special symbols. Note that only the first argument becomes ..

map2(1, 2, function(x,y) x + y)
#[[1]]
#[1] 3

map2(1, 2, ~.x + .y)
#[[1]]
#[1] 3

map2(1, 2, ~..1 + ..2)
#[[1]]
#[1] 3

map2(1, 2, ~. + ..2)
#[[1]]
#[1] 3

map2(1, 2, ~. + .[2])
#[[1]]
#[1] NA

当有很多变量时,这种自动赋值非常有用.

This automatic assignment can be very helpful when there are many variables.

mtcars %>% pmap_dbl(~ ..1/..4)
# [1] 0.19090909 0.19090909 0.24516129 0.19454545 0.10685714 0.17238095 0.05836735 0.39354839 0.24000000 0.15609756
#[11] 0.14471545 0.09111111 0.09611111 0.08444444 0.05073171 0.04837209 0.06391304 0.49090909 0.58461538 0.52153846
#[21] 0.22164948 0.10333333 0.10133333 0.05428571 0.10971429 0.41363636 0.28571429 0.26902655 0.05984848 0.11257143
#[31] 0.04477612 0.19633028

但是除了我上面提到的所有特殊符号之外,参数也分配给了 ....就像所有的 R 一样,... 有点像一个命名的参数列表,所以你可以将它与 with 一起使用:

But in addition to all of the special symbols I noted above, the arguments are also assigned to .... Just like all of R, ... is sort of like a named list of arguments, so you can use it along with with:

mtcars %>% pmap_dbl(~ with(list(...), mpg/hp))
# [1] 0.19090909 0.19090909 0.24516129 0.19454545 0.10685714 0.17238095 0.05836735 0.39354839 0.24000000 0.15609756
#[11] 0.14471545 0.09111111 0.09611111 0.08444444 0.05073171 0.04837209 0.06391304 0.49090909 0.58461538 0.52153846
#[21] 0.22164948 0.10333333 0.10133333 0.05428571 0.10971429 0.41363636 0.28571429 0.26902655 0.05984848 0.11257143
#[31] 0.04477612 0.19633028

另一种思考为什么会这样的方法是因为 data.frames 只是一个带有一些行名称的 list:

An other way to think about why this works is because data.frames are just a list with some row names:

a <- list(a = c(1,2), b = c("A","B"))
a
#$a
#[1] 1 2
#$b
#[1] "A" "B"
attr(a,"row.names") <- as.character(c(1,2))
class(a) <- "data.frame"
a
#  a b
#1 1 A
#2 2 B

这篇关于在 R 中使用波浪号 (~) 和句号 (.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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