Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
985 views
in Technique[技术] by (71.8m points)

r - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...