[Rd] Matrix subassignment with NA subscripts

Saikat DebRoy saikat at stat.wisc.edu
Wed Sep 17 12:29:35 MEST 2003


With a relatively recent r-devel snapshot,

R> x <- matrix(1:9, 3)
R> x1 <- matrix(51:54, 2)
R> x[c(NA, 2, 3), 1:2] <- x1
Error in "[<-"(`*tmp*`, c(NA, 2, 3), 1:2, value = x1) :
         number of items to replace is not a multiple of replacement 
length
R> x2 <- matrix(101:106, 3)
R> x[c(NA, 2, 3), 1:2] <- x2
R> x
      [,1] [,2] [,3]
[1,]    1    4    7
[2,]  101  103    8
[3,]  102  104    9

This was a little surprising to me. I expected the assignment to 
succeed in the first case and either fail or at least give a warning in 
the second case. I also tried this on Splus.

Splus> x <- matrix(1:9, 3)
Splus> x1 <- matrix(51:54, 2)
Splus> x[c(NA, 2, 3), 1:2] <- x1
Splus> x
      [,1] [,2] [,3]
[1,]    1    4    7
[2,]   51   53    8
[3,]   52   54    9
Splus> x2 <- matrix(101:106, 3)
Splus> x[c(NA, 2, 3), 1:2] <- x2
Warning messages:
   Replacement length not a multiple of number of elements to replace 
in: x[c(NA
         , 2, 3), 1:2] <- x2
Splus> x
      [,1] [,2] [,3]
[1,]    1    4    7
[2,]  101  103    8
[3,]  102  104    9



More information about the R-devel mailing list