Skip to content
Prev 22209 / 63421 Next

Strange behaviour of the [[ operator

Martin Maechler <maechler at stat.math.ethz.ch> writes:
Yes, not being related to "[[" is exactly why it is surprising.

I would expect vv[[f(x)]] to print regardless of f's auto-printing
behavior.

f1 <- function(x) x
f2 <- function(x) return(invisible(x))
a <- list(b=5)
a[[f1("a")]]
a[[f2("a")]]
Here's a similar thing:
[1] 1 2 3 4 5
[1] 3

And this can be confusing if one thinks that subsetting is really a
function and behaves like other R functions w.r.t. to treatment of
named arguments:
[,1] [,2]
[1,]    1    3
[2,]    2    4
[1] 2