[R] In praise of "options(warnPartialMatchDollar = TRUE)"
chrishold at psyctc.org
Sun Nov 27 16:12:27 CET 2016
I am just posting this to the list because someone else may one day waste an hour or so because s/he has unknowingly hit a partial match failure using "$". It's my folly that I did but I am surprised that options(warnPartialMatchDollar = TRUE) isn't the default setting.
Here's a bit of reproducible code that shows the challenge.
#rm(list=ls()) ### BEWARE: me making sure environment was clean
set.seed(12345) # get fully reproducible example
nRows <- 100
Sample <- sample(0:1,nRows,replace=TRUE)
data2 <- data.frame(cbind(1:nRows,Sample)) # create data frame
table(data2$Samp) # call which silently achieves partial match
data2$innoccuousname <- factor(data2$Samp,labels=c("Non-clinical","Clinical"),levels=0:1)
str(data2$Samp) # all fine, no apparent destruction of the non-existent vector data2$Samp
data2$SampFac <- factor(data2$Samp,labels=c("Non-clinical","Clinical"),levels=0:1)
str(data2$Samp) # returns NULL because there is no longer a single partial match to "Samp" but no warning!
str(data2$Sample) # but of course, data2$Sample is still there
Because I had used "data2$Samp" all the way through a large file of R (markup) code and hadn't noticed that the variable names in the SPSS file I was reading in had changed from "Samp" to "Sample" I appeared to be destroying data2$Samp.
I have now set options(warnPartialMatchDollar = TRUE) in my Rprofile.site file and am just posting this here in case it helps someone some day.
Very best all,
[[alternative HTML version deleted]]
More information about the R-help