From bbo|ker @end|ng |rom gm@||@com  Tue Aug 11 03:23:01 2020
From: bbo|ker @end|ng |rom gm@||@com (Ben Bolker)
Date: Mon, 10 Aug 2020 21:23:01 -0400
Subject: [Rd] M[cbind()] <- assignment with Matrix object loses attributes
Message-ID: <baa2781d-0371-401d-b8a5-62b5f693eec5@gmail.com>

 ? 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



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