1

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.
vdebuen
  • 31
  • 3
  • 1
    This is normal behavior. The $ operator needs only the first unique part of a column name. See more here: https://stackoverflow.com/questions/32854683/data-frames-in-r-name-autocompletion – jrcalabrese Jan 28 '23 at 18:43

1 Answers1

0

Yes this is completely expected results. Check help file:

?`$`

It reads:

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.

As you can see, $ works on partial mathching. Alternatively, use can use: x[["name", exact = F]] to get the similar results.

In case your case, you can use this to get the same results:

DF[["log_scale_coeff", exact = FALSE]]
Neeraj
  • 1,166
  • 9
  • 21