Skip to content

$ operator is invalid for atomic vectors, returning NULL - what is the right thing to do then?

4 messages · Søren Højsgaard, Brian Ripley, (Ted Harding) +1 more

#
Dear all,
Starting from a recent version of R, the $ became "unusable" on atomic vectors, e.g.
NULL
Warning message:
In x$a : $ operator is invalid for atomic vectors, returning NULL
 
I can of course do
- but that requires more typing (5 characters rather than 2). 
 
Apologies if I've missed a an announcement regarding this, but
1) Is there an alternative to the ['a'] and
2) Why was this change made?
 
Regards
S?ren
#
On Thu, 6 Dec 2007, S?ren H?jsgaard wrote:

            
It has always returned NULL on atomic vectors: see the help page.
E.g. in R 2.0.0 from 2004:
NULL
a
1

The warning was added three versions of R ago: the announcement is in the 
NEWS file for 2.5.0.

USER-VISIBLE CHANGES

     o   Using $ on an atomic vector now raises a warning, as does use
         on an S4 class for which a method has not been defined.

I think you have exemplified the answer to your question '2)': because 
users misunderstood what it did.

In R 2.7.0 this will be an error, since package programmers did not seem 
to be heeding the warnings.
#
On 06-Dec-07 12:04:56, S?ren H?jsgaard wrote:
While Brian has answered your query from a more technical
point of view, there's an implication you should consider.

x$a always (as far as I know)[*] has been "unusable", in that
it returns NULL, and not what you expected to get. The only
thing that's (relatively) new is that you now get a warning.

[*] Certainly as early as R-1.6.2 (Jan 2003), as I've just checked.

Previously, you were not getting a warning.

So, if you were using the likes of x$a in code, and getting
no warnings, possibly your code was generating results
derived from NULL values rather than from the values you
expected it to be using. Perhaps you should check back!

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 06-Dec-07                                       Time: 12:39:49
------------------------------ XFMail ------------------------------
#
On 12/6/2007 7:04 AM, S?ren H?jsgaard wrote:
There's an announcement in the NEWS file for 2.6.0

DEPRECATED & DEFUNCT

     o	$ on an atomic vector now gives a warning that it is 'invalid'.
	It remains deprecated, but may be removed in R >= 2.7.0.

and an update in R-devel saying that in fact it has been made defunct there.

In answer to your questions:

1) I don't think so, other than x[1].
2) I don't remember the details, other than it was to avoid confusion. 
Maybe someone else can summarize the discussion?

Duncan Murdoch