[Rd] Attributes of a vector element?

J C Nash pro|jcn@@h @end|ng |rom gm@||@com
Tue Feb 22 19:29:15 CET 2022


I'm sending this to r-devel rather than r-help as I suspect I'm pushing the boundaries
of the R language. In that I get no errors but things don't "work", I may have revealed
a gap in the attributes structure.

In displaying results of some optimization calculations (in the optimx package I maintain),
I'd like to add indicators F(ree), L(ower) and U(pper) to indicate if parameters returned are
on bounds. I've got my optimr() routine to put the character indicators in the list() structure of
the answer to each individual solver result. However, when I collect these into a data frame
for the results of several solvers (run by the opm() function), it seems that attributes
can't be carried through.

Here is a small example.

pp<-c(1,2)
attr(pp[1], "trial")<-"first"
attributes(pp)
attributes(pp[1])
attr(pp[1],"trial")
pp[1]
pp[2]
attr(pp, "name")<-"rubbish"
attributes(pp)
str(pp)

The output is:

 > pp<-c(1,2)

 > attr(pp[1], "trial")<-"first"  ## APPEARS TO ACCEPT ATTRIBUTE

 > attributes(pp)
NULL

 > attributes(pp[1])
NULL

 > attr(pp[1],"trial")            ## FAILS TO FIND ATTRIBUTE
NULL

 > pp[1]
[1] 1

 > pp[2]
[1] 2

 > attr(pp, "name")<-"rubbish"

 > attributes(pp)
$name
[1] "rubbish"


 > str(pp)
  num [1:2] 1 2
  - attr(*, "name")= chr "rubbish"

Similarly,

pl<-list(one=1, two=2)
attr(pl[1],"trial")<- "lfirst"
attr(pl[1], "trial")
attributes(pl)
attributes(pl[1])
attributes(pl$one)
str(pl)

This also fails.

Am I doing something silly?

Thanks in advance for any pointers.

JN



More information about the R-devel mailing list