[R] Partial matching when using $ ? Feature or bug?

Rainer M Krug r.m.krug at gmail.com
Fri Mar 16 15:01:05 CET 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 16/03/12 14:54, Sarah Goslee wrote:
> Hi Rainer,
> 
> On Fri, Mar 16, 2012 at 9:45 AM, Rainer M Krug <r.m.krug at gmail.com> wrote:
>> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
>> 
>> Hi
>> 
>> I have the following code:
>> 
>> x <- data.frame(x1=1, x2=2, y1=3) x$y [1] 3 x$x NULL
> 
>> 
>> I was surprised (and definitely irritated?) when I realised that partial matching also works
>> for the $. Is this intended?
> 
> This is documented behavior, which by definition makes it a feature, right?

Not necessary - one can also document bugs. But I agree - this has to be considered a feature.

> 
> Both ‘[[’ and ‘$’ select a single element of the list.  The main difference is that ‘$’ does
> not allow computed indices, whereas ‘[[’ does.  ‘x$name’ is equivalent to ‘x[["name", exact = 
> FALSE]]’.  Also, the partial matching behavior of ‘[[’ can be controlled using the ‘exact’
> argument.
> 
> It's also a really good reason to use [[ in functions or anywhere there might be a chance of
> confusion.

Absolutely - I think I have to change my habit. I always prefered using $ as it seemed to me more
logical.

Thanks,

Rainer


> 
> Sarah
> 


- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys.
(Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      Rainer at krugs.de

Skype:      RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9jR6AACgkQoYgNqgF2egqfdwCdGOhe5jk/xgaxpISf9GGMA+Yk
68IAn1RA9N7vJnytAliFszXJxt3/3tJC
=hIXq
-----END PGP SIGNATURE-----



More information about the R-help mailing list