[R] esoteric inconsistency -- intended or not?

Bert Gunter gunter.berton at gene.com
Fri Nov 7 20:18:42 CET 2008


Is the following intended or not?

> func<- function(y) match.call()

> z <- func(y =2)

> z
func(y = 2)

> z[["a"]] <- 5

> z
func(y = 2, 5) ## Note that the second argument **is not** named

## BUT...

> z <- func(y =2)

> z$a <- 5

> z
func(y = 2, a = 5) ## The second argument **is** named

### End of example code ###

The reason I ask is that the man page for [[ specifically says:
**************
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. 

[ and [[ are sometimes applied to other recursive objects such as calls and
expressions. Pairlists are coerced to lists for extraction by [, but all
three operators can be used for replacement.
 
********
 I (mis?)read this as saying the behavior in the code snippets above should
produce identical results.

I note that the above inconsistency can be trivially avoided by first
coercing the call object to a list, modifying it either way, and then
coercing it back to a call object.

I doubt if it makes a difference, but:

> version
               _                                          
platform       i386-pc-mingw32                            
arch           i386                                       
os             mingw32                                    
system         i386, mingw32                              
status         Patched                                    
major          2                                          
minor          8.0                                        
year           2008                                       
month          10                                         
day            23                                         
svn rev        46779                                      
language       R                                          
version.string R version 2.8.0 Patched (2008-10-23 r46779)

Cheers,
Bert Gunter



More information about the R-help mailing list