From ru|pb@rr@d@@ @end|ng |rom @@po@pt  Wed Sep  9 08:03:47 2020
From: ru|pb@rr@d@@ @end|ng |rom @@po@pt (Rui Barradas)
Date: Wed, 9 Sep 2020 07:03:47 +0100
Subject: [Rd] more Matrix weirdness
In-Reply-To: <fbaf45b7-b6d8-e93f-39d9-aa51d6f2c28e@gmail.com>
References: <fbaf45b7-b6d8-e93f-39d9-aa51d6f2c28e@gmail.com>
Message-ID: <7037975c-22b6-6eca-d871-743eead534f2@sapo.pt>

Hello,

R 4.0.2 on Ubuntu 20.04, sessionInfo() below.

I can reproduce this, sort of. The error I'm getting is different:


x[rr, cc] <- m
#Error in x[rr, cc] <- m : replacement has length zero

But if I check lengths and dimensions, they are identical().

identical(length(x[rr, cc]), length(m))
#[1] TRUE
identical(dim(x[rr, cc]), dim(m))
#[1] TRUE


What works is


x[rr, cc] <- as.matrix(m)

I ran Ben's code on RStudio 1.3.1073, the following is run with Rscript 
and the error message is the same.


rui at rui:~$ Rscript --vanilla rhelp.R
R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
  [1] LC_CTYPE=pt_PT.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=pt_PT.UTF-8        LC_COLLATE=pt_PT.UTF-8
  [5] LC_MONETARY=pt_PT.UTF-8    LC_MESSAGES=pt_PT.UTF-8
  [7] LC_PAPER=pt_PT.UTF-8       LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] Matrix_1.2-18

loaded via a namespace (and not attached):
[1] compiler_4.0.2  grid_4.0.2      lattice_0.20-41
Error in x[rr, cc] <- m :
   number of items to replace is not a multiple of replacement length
Execution halted


Hope this helps,

Rui Barradas


?s 03:04 de 09/09/20, Ben Bolker escreveu:
>  ? Am I being too optimistic in expecting this (mixing and matching 
> matrices and Matrices) to work?? If x is a matrix and m is a Matrix, 
> replacing a commensurately sized sub-matrix of x with m throws "number 
> of items to replace is not a multiple of replacement length" ...
> 
> x <- matrix(0,nrow=3,ncol=10, dimnames=list(letters[1:3],LETTERS[1:10]))
> rr <- c("a","b","c")
> cc <- c("B","C","E")
> m <- Matrix(matrix(1:9,3,3))
> x[rr,cc] <- m
> 
>  ?? cheers
>  ??? Ben Bolker
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] more Matrix weirdness
Rui Barradas