Toby Muhlhofer Toby Muhlhofer - 2 months ago 6
R Question

Disabling partial variable names in subsetting data frames

I have just discovered that R will allow me to use partial variable names to subset data frames, as long as they are uniquely defined. I find this dangerous and am wondering if there is a way (like an environment variable or something) to disable this behavior.

Here is what I mean:

myframe <- data.frame(othervar=1:3, myvar=4:6)
print(myframe$myv)

[1] 4 5 6


What I would like to happen is the same as for

print(myframe$wrong)

NULL


Any way to change this?

Answer

No, not really. You can use options(warnPartialMatchDollar=TRUE) to get a warning when you do this (I'm using r-devel ... I think this is in the released version??), and you could use options(warn=2) to upgrade warnings to errors (but this would upgrade all warnings to errors ...)

I believe the standard advice/best practice is to use [[-indexing instead

myframe <- data.frame(othervar=1:3, myvar=4:6)
myframe$myv
## [1] 4 5 6   (no problem)
myframe$wrong
## NULL

options(warnPartialMatchDollar=TRUE)
myframe$myv
## [1] 4 5 6
## Warning message:
## In `$.data.frame`(myframe, myv) :
##   Partial match of 'myv' to 'myvar' in data frame
options(warn=2) ## upgrade warnings to errors
myframe$myv
## Error in `$.data.frame`(myframe, myv) : 
##   (converted from warning) Partial match of 'myv' to 'myvar' in data frame

myframe[["myv"]]
## NULL