开发者

"..1" in the body of "[[.data.frame"

开发者 https://www.devze.com 2023-03-31 13:45 出处:网络
When I look at the contents of [[.data.frame on my PC, this is what I get: > get(\"[[.data.frame\")

When I look at the contents of [[.data.frame on my PC, this is what I get:

> get("[[.data.frame")
function (x, ..., exact = TRUE) 
{
    na <- nargs() - (!missing(exact))
    if (!all(names(sys.call()) %in% c("", "exact"))) 
        warning("named arguments other than 'exact' are discouraged")
    if (na < 3L) 
        (function(x, i, exact) if (is.matrix(i)) 
            as.matrix(x)[[i]]
        else .subset2(x, i, exact = exact))(x, ..., exact = exact)
    else {
        col <- .subset2(x, ..2, exact = exact)
        i <- if (is.character(..1)) 
            pmatch(..1, row.names(x), duplicates.ok = TRUE)
        else ..1
        .subset2(col, i, exact = exact)
    }
}
<environment: namespace:base>

I've gotten开发者_JAVA百科 used to ..., but this is the first time I saw ..1 and ..2. A quick search both in R help and Google returned mostly rubbish, since the dots are often interpreted as placeholders, so I'm hoping someone here can give me a pointer? Or am I missing something dreadfully obvious? Where do these come from and how can I use them?


This is a way to reference the 1st, 2nd, ... elements of the special pairlist that is .... So ..1 is the way to refer to the first element of ..., ..2 refers to the second element of ... and so on.

This is mentioned in the R Internals manual in section 1.5.2 Dot-dot-dot arguments, the relevant bit of which is:

The value of ... is a (special) pairlist whose elements are referred to by the special symbols ..1, ..2, ... which have the DDVAL bit set: when one of these is encountered it is looked up (via ddfindVar) in the value of the ... symbol in the evaluation frame.


To add to Gavin's answer:

They are also mentioned briefly in the help page for reserved words (?Reserved).

A really simple example of usage is

f <- function(...) print(..1)
f(x = 99)  #prints 99
f()        #throws an error
0

精彩评论

暂无评论...
验证码 换一张
取 消