[R] In praise of "options(warnPartialMatchDollar = TRUE)"

David Winsemius dwinsemius at comcast.net
Sun Nov 27 19:25:18 CET 2016


> On Nov 27, 2016, at 7:12 AM, Chris Evans <chrishold at psyctc.org> wrote:
> 
> 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 

Using dataframe( cbind( ...) ) is a predictable method for creating later headaches. and there is no options-warning available. cbind coerces an argument list of vectors to matrix class, thus dropping all attributes (dates, times and factors are all destroyed.)

> 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. 

This is one of the reasons many experienced R programmers eschew the use of the "$" function in programming.

The preferred use would be :

data2[['Samp']]

(No partial match.)

> 
> 	[[alternative HTML version deleted]]

Plain text is generally preferred on Rhelp but there does not appear to have been a problem in this posting instance.

> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list