Re: [R] $ operator is invalid for atomic vectors, returning NULL - what is the right thing to do then?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu, 6 Dec 2007 12:22:39 +0000 (GMT)

On Thu, 6 Dec 2007, Søren Højsgaard wrote:

> Dear all,
> Starting from a recent version of R,

>From R 2.5.0, not so recent.

> the $ became "unusable" on atomic vectors, e.g.
>> x <- c(a=1,b=2)
>> x$a
> NULL
> Warning message:
> In x$a : $ operator is invalid for atomic vectors, returning NULL
> I can of course do
>> x['a']
> - 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?

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

> x <- c(a=1,b=2)
> x$a
NULL
> x['a']

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.

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ R-help_at_r-project.org mailing list 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.

Received on Thu 06 Dec 2007 - 12:26:03 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 06 Dec 2007 - 12:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.