0

In R code example 1:

mat <- matrix(1, nrow = 4, ncol = 4)
avec <- c(2, 3)
mat[avec, avec] <- 0

mat is:

1    1    1    1
1    0    0    1
1    0    0    1
1    1    1    1

But in example 2:

res <- avec + avec

res is (4, 6)

In other words, in example 1, recycling avec generates the pairs (2, 2), (2, 3), (3, 2), and (3, 3), while in example 2, the pairs are (2, 2) and (3, 3). Isn't the recycling inconsistent in these two examples?

Neal Oden
  • 57
  • 4
  • Zheyuan, thanks very much for your careful exposition, and for the excellent reference. Sorry for the sloppy use of "recycling". I lack R vocabulary. I was trying to develop an intuition for how R might default in terms of handling expressions with multiple vectors in them, when there is nothing obvious in the expression to indicate how. But maybe there is no such default. – Neal Oden Jul 06 '22 at 13:33
  • For example, paste(c("X","Y"), 1:10, sep="") produces X1, Y2, X3, etc. One might expect on those grounds that [avec, avec] in example 1 would produce (2,2) and (3,3), but it doesn't. – Neal Oden Jul 08 '22 at 17:29

0 Answers0