Why doesn't R throw an error when I use only the initial part of my column name in a data frame?

Viewed 88

I have a data frame containing various columns along with sender_bank_flag. I ran the below two queries on my data frame.

sum(s_50k_sample$sender_bank_flag, na.rm=TRUE)

sum(s_50k_sample$sender_bank, na.rm=TRUE)

I got the same output from both the queries even though there is no such column as sender_bank in my data frame. I expected to get an error for the second code. Didn't know R has such a functionality! Does anyone know what exactly is this functionality & how can it be better utilized?

1 Answers

Probably worthwhile to augment all comments into an answer.


Both my comment and BenBolker's point to doc page ?Extract:

Under Recursive (list-like) objects:

Both "[[" and "$" select a single element of the list. The main difference is that "$" does not allow computed indices, whereas "[[" does. x$name is equivalent to x[["name", exact = FALSE]]. Also, the partial matching behavior of "[[" can be controlled using the exact argument.

Under Character indices:

Character indices can in some circumstances be partially matched (see ?pmatch) to the names or dimnames of the object being subsetted (but never for subassignment). Unlike S (Becker et al p. 358), R never uses partial matching when extracting by "[", and partial matching is not by default used by "[[" (see argument exact).

Thus the default behaviour is to use partial matching only when extracting from recursive objects (except environments) by "$". Even in that case, warnings can be switched on by options(warnPartialMatchDollar = TRUE).

Note, the manual has rich information, and make sure you fully digest them. I formatted the content, adding Stack Overflow threads behind where relevant.


Links provided by phiver's comment are worth reading in a long term.

Related