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

Ben Bolker bbo|ker @end|ng |rom gm@||@com
Tue Aug 11 03:23:01 CEST 2020


   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



More information about the R-devel mailing list