Skip to content

problem with attr()

2 messages · Gardar Johannesson, Brian Ripley

#
I got this wired behaviour of the attr() function using R-1.6.1 on both 
linux redhat 7.3 (i386) and Solaris 8 (Sparc):

 > tmp <- list(id=1)
 >
 > attr(tmp,"n.ch") <- 2
 > attr(tmp,"n") <- 1
 > tmp
$id
[1] 1

attr(,"n.ch")
[1] 2
attr(,"n")
[1] 1
 >
 > attributes(tmp)
$names
[1] "id"

$n.ch
[1] 2

$n
[1] 1

 > attr(tmp,"names")
[1] "id"
 > attr(tmp,"n.ch")
[1] 2
 > attr(tmp,"n")
NULL
 >

The attr()function doesn't find the 'n' attribute, however, if I don't 
create the 'n.ch' attribute, it finds it!  That is:

 > tmp <- list(id=1)
 > attr(tmp,'n')
NULL
 > attr(tmp,'n') <- 1
 > attr(tmp,'n')
[1] 1
 >
#
It's a bug in do_attr.  Although not documented, this looks for a partial
match as well as a full match.  *Unfortunately* if it finds two partial
matches (as in your example) it gives up and returns NULL.  That is
correct unless there is an exact match later ....

I am about to put a fix in R-patched.
On Wed, 27 Nov 2002, Gardar Johannesson wrote: