This got me pretty bad. You can abbreviate list names? I never noticed it before and I got totally screwed for like a day. Can someone explain what is happening here and why it could be more useful than it is terrible? And why its inconsistent like that at the bottom? And if I can turn it off?
> wtf <- list(whatisthe=1, pointofthis=2)
> wtf$whatisthe
[1] 1
> wtf$what
[1] 1
> wtf <- list(whatisthe=1, whatisthepointofthis=2)
> wtf$whatisthepointofthis
[1] 2
> wtf$whatisthep
[1] 2
> wtf$whatisthe
[1] 1
> wtf$what
NULL
For list element names (and function parameter names), R applies the following algorithm:
If there is an exact match for the item, use it. If there is not an exact match, look for partial matches. If there is exactly one partial match, use it. Otherwise, use nothing.
I suspect that partial matching by the
$
operator was a nice(r) feature for interactive use back in the days before tabbed completion had been implementedIf you don't like that behavior, you can use the
"[["
operator instead. It takes an argumentexact=
, which allows you to control partial matching behavior, and which defaults toTRUE
.(This is one reason why
"[["
is generally preferred to$
in R programming. Another is the ability to do thisX <- "whatisthe"; wtf[[X]]
but not thisX <- "whatisthe"; wtf$X
.)