除了部分匹配之外,$ 运算符可以做任何 [ 和 [[ 做不到的事情吗?

Aside from partial matching, can the $ operator do anything that [ and [[ cannot?

我相信 $ 运算符的以下内容是正确的:

这是否意味着如果我不关心部分匹配,我总是可以将 $ 替换为 [[[?还是我错过了一些功能?

根据一本关于​​高级 R 的书,除了部分匹配之外,$ 和 [ 运算符在数据帧(而不是列表)上是相同的。它指出

$ is a shorthand operator: x$y is roughly equivalent to x[["y"]]. ...The one important difference between $ and [[ is that $ does (left-to-right) partial matching:

引用如下:下一个 link 的第 4.3.2 节: https://adv-r.hadley.nz/subsetting.html#section-1

对于 base R,我最好的猜测来自 the documentation for $。以下引述是最相关的:

$ is only valid for recursive objects

$ does not allow computed indices, whereas [[ does. x$name is equivalent to x[["name", exact = FALSE]]. Also, the partial matching behavior of [[ can be controlled using the exact argument.

the default behaviour is to use partial matching only when extracting from recursive objects (except environments) by $. Even in that case, warnings can be switched on by options(warnPartialMatchDollar = TRUE).

所以文档似乎证实了我的信念,即除了部分匹配之外,$ 只是语法糖。但是,有四点我不确定:

  1. 我从不过于相信 R 的文档。因此,我相信有经验的用户一定能找出我所说的漏洞。
  2. 我说这只是我对基数 R 的猜测,因为 $ 是一个通用运算符,因此可以通过包更改其含义,tibbles 是一个常见的例子。
  3. $[也可以用于环境,但我没见过有人这样做。
  4. 我不知道“计算索引”是什么。