Re: ran into ambiguous code (bug?)
Martin Maechler <[email protected]> Tue, 28 Jul 2026 07:53:27 +0200
| Newsgroups | gmane.comp.lang.r.general |
|---|---|
| Message-ID | <[email protected]> |
Dear Ben,
As this may be instructive for some in the R-help audience,
I'm replying here in public (and Bcc to you):
>>>>> Ben H.... on Mon, 27 Jul 2026 16:50:20 -0700 writes:
> Hi,
> I would like to submit a bug report or at least get some help. I've used R
> for about 10 years and not reported a bug before. Today I ran into
> ambiguous R code, the code is reading non-existent columns. A reproducible
> example of the bug along with the R version report is below. 'Age' does
> not exist among the column names of the data frame (df), but 'Age2' does.
> When you run df$Age, it is equivalent to df$Age2. I have reproduced this
> issue after removing all packages.
> 1. What should I do to use R without this issue?
> 2. Can/should I submit this?
> R version 4.5.2 (2025-10-31) -- "[Not] Part in a Rumble"
> Copyright (C) 2025 The R Foundation for Statistical Computing
> Platform: aarch64-apple-darwin20
......
>> df <- data.frame(people = c('Susan', 'Mike', 'Gary', 'Wayne'),
> Age2 = c(2,24,35,1))
>>
>> df$Age2
> [1] 2 24 35 1
>> df$Age
> [1] 2 24 35 1
>>
>> "Age2" %in% colnames(df)
> [1] TRUE
>> "Age" %in% colnames(df)
> [1] FALSE
>>
> --
> Ben H....., PhD
Dear Ben,
You are reporting a problem you have with R, but not at all bug:
This is how R has worked forever (and S & S-plus before that).
Note:
- column names can be abbreviated when using `$` (hence you can
use `$Age` equivalently to `$Age2`
- Since this maybe confusing to some, you can make it to a warning via
options(warnPartialMatchDollar = TRUE)
Historical reason:
In earlier times, before people had IDEs such as Rstudio etc,
VScode, or Emacs(ESS) to do data analysis in R, writing long
variable names was cumbersome and hence allowed to be abbreviated.
In your example :
> options(warnPartialMatchDollar = TRUE)
> df$Age
[1] 2 24 35 1
Warning message:
In df$Age : partial match of 'Age' to 'Age2'
Note that I recommend using df[,"Age2"] (which works in your
example) whereas df[,"Age"] gives an error :
> df[,"Age"]
Error in `[.data.frame`(df, , "Age") : undefined columns selected
We have looked into this (and related "partial matching"), and
found that it would "break" too much old R code, if the warning
was turned on unconditionally.
You may be interested in reading help(options) and search for
other 'warnPartialMatch*' ..
e.g., only at
https://cran.r-project.org/doc/manuals/r-release/packages/base/refman/base.html#options
Best regards,
Martin
--
Martin Maechler
R Core team and ETH Zurich