本文介绍了什么!运算符在R中表示平均值,尤其是在上下文中!! sym("x")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"!!"是什么意思在R中使用,为什么要使用它?

What does "!!" do in R, and why would you use it?

具体来说,我正在研究一个涉及以下短语的函数a = !!sym("x")其中"x"是字符串.我以为sym通过将字符串转换为对象来工作,因此a = sym("x")会将a设置为等于对象x. !!有什么用?我读到它后面没有引号,但我认为sym本身没有引号的字符串吗?

Specifically, I'm looking at a function that involves the phrasea = !!sym("x") where "x" is a string. I thought sym worked by turning a string into an object, so a = sym("x") would set a equal to the object x. What is the !! there for? I read that it unquotes whatever is after it, but I thought sym itself unquoted strings?

我还看到!!与其他功能一起使用.在做什么?

I also see !! used with other functions. What is it doing?

推荐答案

当您将字符串转换为符号时,它打印时不带引号,但这并不是取消引号的含义(我们将在最后进行讨论) .

When you convert a string to a symbol, it prints without the quotes, but that's NOT what unquoting means (we'll get back to that in the end).

rlang::sym()正在根据字符串创建符号,它与base::as.symbol()几乎相同(与该答案无关的微小差异),它本身是base::as.name()的别名:

rlang::sym() is creating a symbol from a string, it's almost the same as base::as.symbol() (tiny differences irrelevant to this answer), itself an alias for base::as.name() :

nm <- "Sepal.Width"
x <- rlang::sym(nm)
x
#> Sepal.Width
typeof(x)
#> [1] "symbol"
identical(x, as.symbol(nm))
#> [1] TRUE

这些无效,因为xnm分别是符号和字符,所以我不能将它们乘以2:

Those don't work, as x and nm are respectively a symbol and a character, so I can't multiply them by 2:

dplyr::mutate(head(iris),SW2 = nm * 2)
#> Error in nm * 2: argument non numérique pour un opérateur binaire
dplyr::mutate(head(iris),SW2 = x * 2)
#> Error in x * 2: argument non numérique pour un opérateur binaire

!!本身并不做任何事情,也不是真正的运算符,但是它会告诉mutate()做某事,因为mutate()旨在识别它.

!! doesn't do anything by itself and is not a real operator, it tells mutate() to do something though, because mutate() is designed to recognize it.

它告诉mutate()的行为是好像!!x被x的引用内容替换了.

What it tells to mutate() is to act as if !!x was replaced by the quoted content of x.

# equivalent to dplyr::mutate(head(iris), Sepal.Width * 2)
dplyr::mutate(head(iris), !!x * 2)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
#>   Sepal.Width * 2
#> 1             7.0
#> 2             6.0
#> 3             6.4
#> 4             6.2
#> 5             7.2
#> 6             7.8

dplyr::mutate(head(iris), !!sym("Sepal.Width") * 2)将给出相同的输出.

通过查看其他等效调用,可能更容易理解为什么取消报价:

Why it is called unquoting might be easier to understand by looking at this other equivalent call :

quoted <- quote(Sepal.Width * 2)
dplyr::mutate(head(iris), !!quoted)

有关更多详细信息,请参见help("!!").

See help("!!") for more details.

这篇关于什么!运算符在R中表示平均值,尤其是在上下文中!! sym("x")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 02:24