R中的Tilde(〜)和句点(.)的使用 [英] Use of Tilde (~) and period (.) in R

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

问题描述

我要使用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.

因此,在编写循环或使用map()而不是写出function()时,可以使用波浪号代替〜.

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.frame s只是具有某些行名的 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中的Tilde(〜)和句点(.)的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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