M[cbind()] <- assignment with Matrix object loses attributes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

M[cbind()] <- assignment with Matrix object loses attributes

bbolker
   Does this constitute a bug, or is there something I'm missing?
assigning sub-elements of a sparse Matrix via M[X]<-..., where X is a
2-column matrix, appears to drop user-assigned attributes. I dug around
in the R code for Matrix trying to find the relevant machinery but my
brain started to hurt too badly ...

    Will submit this as a bug if it seems warranted.

library(Matrix)
m1 <- matrix(1:9,3,3)
m1 <- Matrix(m1)
attr(m1,"junk") <- 12
stopifnot(isTRUE(attr(m1,"junk")==12))  ## OK
m1[cbind(1:2,2:3)] <- 1
stopifnot(isTRUE(attr(m1,"junk")==12)) ## not OK
attr(m1,"junk") ## NULL


## note I have to use the ugly stopifnot(isTRUE(...)) because a missing
attribute returns NULL, an assignment to NULL returns NULL, and
stopifnot(NULL) doesn't stop ...


    cheers

      Ben Bolker

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: M[cbind()] <- assignment with Matrix object loses attributes

Abby Spurdle
Hi Ben,

I had some problems reproducing this.
As far as I can tell *all* indexed assignments drop attributes.
(Maybe we have different versions).

I'm not an expert on S4, but I'm unenthusiastic about mixing slot (S4)
semantics with attribute (S3) semantics.
And str() excludes attributes, but attributes() includes slots.
Highlighting the problems here...

I think R should generate an error or a warning, if a user tries to
assign attributes to S4 objects.

In saying that, mixing OO design with numerical linear algebra is a gold mine...


On Tue, Aug 11, 2020 at 1:23 PM Ben Bolker <[hidden email]> wrote:

>
>    Does this constitute a bug, or is there something I'm missing?
> assigning sub-elements of a sparse Matrix via M[X]<-..., where X is a
> 2-column matrix, appears to drop user-assigned attributes. I dug around
> in the R code for Matrix trying to find the relevant machinery but my
> brain started to hurt too badly ...
>
>     Will submit this as a bug if it seems warranted.
>
> library(Matrix)
> m1 <- matrix(1:9,3,3)
> m1 <- Matrix(m1)
> attr(m1,"junk") <- 12
> stopifnot(isTRUE(attr(m1,"junk")==12))  ## OK
> m1[cbind(1:2,2:3)] <- 1
> stopifnot(isTRUE(attr(m1,"junk")==12)) ## not OK
> attr(m1,"junk") ## NULL
>
>
> ## note I have to use the ugly stopifnot(isTRUE(...)) because a missing
> attribute returns NULL, an assignment to NULL returns NULL, and
> stopifnot(NULL) doesn't stop ...
>
>
>     cheers
>
>       Ben Bolker
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: M[cbind()] <- assignment with Matrix object loses attributes

bbolker
   Thanks for taking a look!

    Hmm, really?  In `R Under development (unstable) (2020-08-14
r79020)`, doing the indexed assignment with a regular matrix (as opposed
to a Matrix) appears to preserve attributes.

m1 <- matrix(1:9,3,3)
attr(m1,"junk") <- 12
stopifnot(isTRUE(attr(m1,"junk")==12)) ## OK
m1[cbind(1:2,2:3)] <- 1
stopifnot(isTRUE(attr(m1,"junk")==12)) ## OK
attr(m1,"junk")  ## 12

    Do you lose attributes with this code? It would surprise me if this
had changed in recent versions but I guess anything's possible ...

On 8/22/20 3:36 AM, Abby Spurdle wrote:

> Hi Ben,
>
> I had some problems reproducing this.
> As far as I can tell *all* indexed assignments drop attributes.
> (Maybe we have different versions).
>
> I'm not an expert on S4, but I'm unenthusiastic about mixing slot (S4)
> semantics with attribute (S3) semantics.
> And str() excludes attributes, but attributes() includes slots.
> Highlighting the problems here...
>
> I think R should generate an error or a warning, if a user tries to
> assign attributes to S4 objects.
>
> In saying that, mixing OO design with numerical linear algebra is a gold mine...
>
>
> On Tue, Aug 11, 2020 at 1:23 PM Ben Bolker <[hidden email]> wrote:
>>
>>     Does this constitute a bug, or is there something I'm missing?
>> assigning sub-elements of a sparse Matrix via M[X]<-..., where X is a
>> 2-column matrix, appears to drop user-assigned attributes. I dug around
>> in the R code for Matrix trying to find the relevant machinery but my
>> brain started to hurt too badly ...
>>
>>      Will submit this as a bug if it seems warranted.
>>
>> library(Matrix)
>> m1 <- matrix(1:9,3,3)
>> m1 <- Matrix(m1)
>> attr(m1,"junk") <- 12
>> stopifnot(isTRUE(attr(m1,"junk")==12))  ## OK
>> m1[cbind(1:2,2:3)] <- 1
>> stopifnot(isTRUE(attr(m1,"junk")==12)) ## not OK
>> attr(m1,"junk") ## NULL
>>
>>
>> ## note I have to use the ugly stopifnot(isTRUE(...)) because a missing
>> attribute returns NULL, an assignment to NULL returns NULL, and
>> stopifnot(NULL) doesn't stop ...
>>
>>
>>      cheers
>>
>>        Ben Bolker
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: M[cbind()] <- assignment with Matrix object loses attributes

Abby Spurdle
> Hmm, really?  In `R Under development (unstable) (2020-08-14
> r79020)`, doing the indexed assignment with a regular matrix (as opposed
> to a Matrix) appears to preserve attributes.

I was referring to *Matrix* objects.
Sorry, if that wasn't clear.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel