It seems that the R operator $ looks for a field name similar to the requested one if it does not exist, which seems to me to be a totally inexperienced and very dangerous behaviour.
I have reproduced the following code in two versions of R
- R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting".
- R version 3.6.3 (2020-02-29) -- "Holding the Windsock".
R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Previously saved workspace restored]
No package is required and all possibly loaded variables are deleted even though it is a new R session and the environment was previously empty.
> rm(list=ls())
A very simple data.frame is defined with two fields
> DF = data.frame(
+ other_field = c(0,1),
+ log_scale_coeff_diff = c(1,2)
+ )
The log_scale_coeff field does not exist, so this expression returns an error as expected
> DF[,"log_scale_coeff"]
Error in `[.data.frame`(DF, , "log_scale_coeff") :
undefined columns selected
BUT THIS OTHER EQUIVALENT EXPRESSION DOESN'T RETURN ANY ERROR
> DF$log_scale_coeff
[1] 1 2
MYSTERIOUSLY RETURNS THE VALUE OF ANOTHER FIELD WHOSE NAME CONTAINS THE REQUESTED FIELD THAT DOES NOT EXIST. !!!!!!!
> DF$log_scale_coeff_diff
[1] 1 2
Is this really the expected result? I can't believe it.
I would like to know if this is really the expected behaviour or if it is a strange and unknown bug.