[R] In praise of "options(warnPartialMatchDollar = TRUE)"
chrishold at psyctc.org
Sun Nov 27 20:21:17 CET 2016
Was about to reply just to sender but thought there were good byproducts of this so, to all ...
Many thanks Dr. Winsemius, and apologies for the HTML: tired sloppiness. My bad.
Aha. For the first time I can really see a logic for dataFrame[['variable']] -- thanks for that. I will have to break myself of the "$" habit. Pity as it's a lot more keystrokes! W
I understand about the nasty asset stripped side effects of cbind but thought in this situation it would cause no problems. Is the preferred route to create turn a first vector to data frame and then add the others using dataframe[['newVariable']] <- nextVector ?
Very best wishes and thanks again: this is an amazing list,
----- Original Message -----
> From: "David Winsemius" <dwinsemius at comcast.net>
> To: "Chris Evans" <chrishold at psyctc.org>
> Cc: r-help at r-project.org
> Sent: Sunday, 27 November, 2016 18:25:18
> Subject: Re: [R] In praise of "options(warnPartialMatchDollar = TRUE)"
>> 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 <-
>> str(data2$Samp) # all fine, no apparent destruction of the non-existent vector
>> data2$SampFac <-
>> 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 :
> (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
>> 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