For named vectors and matrices, does [[ ever use partial matching without passing the exact=FALSE argument?

Viewed 45

I've recently read this section of the R Language Definition and become very confused. It says:

For vectors and matrices the [[ forms are rarely used, although they have some slight semantic differences from the [ form (e.g. it drops any names or dimnames attribute, and that partial matching is used for character indices).

Am I misreading it? It seems to say that [[ allows partial matching (presumably by default) for named vectors and matrices. However, I know this to be obviously false:

> lett<-setNames(sample(26),paste0(letters,letters,letters))
> lett[["aaa"]]#No partial match
[1] 23
> lett[["a"]]#Error
Error in lett[["a"]] : subscript out of bounds

so what did the language definition mean? It could be talking about the exact=FALSE flag that you can set, but this seems a very strange way to do that. In fact, a later section makes the following claim, which my above code disproves, so I'm even more confused:

For [[ and $ partial matching is used if exact matching fails, so x$aa will match x$aabb if x does not contain a component named "aa" and "aabb" is the only name which has prefix "aa". For [[, partial matching can be controlled via the exact argument which defaults to NA indicating that partial matching is allowed, but should result in a warning when it occurs.

Is the language definition simply out of date?

1 Answers

Actually, I think the language definition is - at least partially - indeed out of date. The help page of help("[[") regarding the exact argument states

Controls possible partial matching of [[ when extracting by a character vector [...]. The default is no partial matching. Value NA allows partial matching but issues a warning when it occurs. Value FALSE allows partial matching without any warning.

Usage supports this claim:

x[[i, exact = TRUE]]
x[[i, j, ..., exact = TRUE]]

The following code proves these defaults, as well.

set.seed(1)
lsub <- letters[1:3]
lett <- setNames(lapply(sample(3), c), paste0(lsub, lsub, lsub))
lett
#> $aaa
#> [1] 1
#> 
#> $bbb
#> [1] 3
#> 
#> $ccc
#> [1] 2

# partial matching
lett$a
#> [1] 1
lett[["aa", exact = FALSE]]
#> [1] 1

# no partial matching
lett[["aa"]]
#> NULL

# partial matching with warning
lett[["aa", exact = NA]]
#> Warning in lett[["aa", exact = NA]]: partial match of 'aa' to 'aaa'
#> [1] 1
Related