R中有两个圆点".."

pap*_*rin 25 r

这可能是一个蹩脚的问题,但无论如何.在RStudio中,我只是注意到键入一个数字,......和另一个数字会将字符的语法高亮显示从海军蓝变为淡蓝色以及...之后的数字.

例如,这是一个具有这种独特颜色的数字:

4..4
Run Code Online (Sandbox Code Playgroud)

部分"..4"具有浅蓝色.

我使用默认语法着色.我尝试在解释器中引入这样的常量但我只得到错误"错误:"4..5"中的意外数字常量,并且带有"两个点"或...的查询似乎不是非常友好的谷歌.

有谁知道".."的用法是什么,如果有的话?

Sim*_*lon 29

..4将是R的解析器中的保留字.根据?Reserved你会发现

...并且..1,..2等等,这是用来指从调用函数传递下去参数.

#  Function will return nth element from ... ( n MUST be a named argument)
f <- function( ... , n = NULL )
   return( eval( parse( text = paste0( ".." , n ) ) ) )

#  Return third element of ...
f( n = 3 , 1:3 , 3:1 , 10:15 )
#[1] 10 11 12 13 14 15

#  Try to return element that is out of bounds
f( n = 4 , 1:3 , 3:1 , 10:15 )
#Error in eval(expr, envir, enclos) : 
#  the ... list does not contain 4 elements
Run Code Online (Sandbox Code Playgroud)

既然你知道它是什么,你如何使用它?由John Chambers友情提供;

"这个名称..1是指第一个匹配的参数,..2第二个是等等.你应该避免使用这种模糊的约定,这通常可以通过编写一个带有一些普通参数名的函数来完成,然后调用它"...""

数据分析软件:使用R编程,John M. Chambers,Springer-Verlag,纽约,2008年.
摘录自第457页.

  • 我想我很高兴我*不知道这一点 - 我更害怕在要求时出现问题,例如`.``,而不是我的克苏鲁! (2认同)

Ric*_*ton 12

..n指的是第n个元素....

这是一个稍微简单的替代Simon的答案,避免使用eval/parse.

f <- function(...)
{
  message("dots = ")
  print(list(...))   # Notice that you need to convert ...
                     # to a list or otherwise evaluate it
  message("..1 = ")
  print(..1)
  message("..2 = ")
  print(..2)
}

f(runif(5), letters[1:10])
## dots = 
## [[1]]
## [1] 0.94707123 0.09626337 0.41480592 0.83922757 0.94635464
## 
## [[2]]
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
## 
## ..1 = 
## [1] 0.94707123 0.09626337 0.41480592 0.83922757 0.94635464
## ..2 = 
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
Run Code Online (Sandbox Code Playgroud)


The*_*Pea 8

接受的答案是有道理的。

\n\n

但我在完全不同的上下文中看到了两个点(即不在函数内),如下所示..prop..::

\n\n
ggplot(data=diamonds) + \n  geom_bar(\n   mapping=aes(x=cut, y=..prop.., group=1) \n  )\n
Run Code Online (Sandbox Code Playgroud)\n\n

原来..prop..是由\ 的变换创建的特殊变量ggplotstat_count

\n\n
\n

stat_count提供两个内部变量..count....prop..,分别指计数和比例。不要对这个符号感到惊讶..name..,它用于防止与您自己的列混淆(不要用奇怪的名称命名您自己的列,例如..count..!)

\n
\n\n

(记住R 中的变量名可以包含句点。我有 Python 背景,所以这个双句点看起来像 Python 的双下划线约定:__prop__,一种用于标记特殊/“私有”变量/“名称修饰”的技术)变量

\n

  • 心碎了。R 就像一个语法奇怪的爆炸厨房水槽。 (7认同)