[Rd] No error when assigning values to an "empty" vector/matrix/array

Henrik Bengtsson hb at biostat.ucsf.edu
Fri Oct 24 05:10:19 CEST 2014


Assigning one or more values to a vector/matrix/array x for which
length(x) == 0 gives no error, e.g.

> x <- integer(0)
> x[] <- 1:2

> x <- matrix(nrow=0, ncol=1)
> x[] <- 1:2
> x[,1] <- 1:2

> x <- array(dim=c(0,1,1))
> x[] <- 1:2
> x[,1,1] <- 1:2

whereas

> x <- integer(1)
> x[] <- 1:2
Warning message:
In x[] <- 1:2 :
  number of items to replace is not a multiple of replacement length
> x <- matrix(nrow=1, ncol=1)
> x[] <- 1:2
Warning message:
In x[] <- 1:2 :
  number of items to replace is not a multiple of replacement length
> x[,1] <- 1:2
Error in x[, 1] <- 1:2 :
  number of items to replace is not a multiple of replacement length
> x <- array(dim=c(1,1,1))
> x[] <- 1:2
Warning message:
In x[] <- 1:2 :
  number of items to replace is not a multiple of replacement length
> x[,1,1] <- 1:2
Error in x[, 1, 1] <- 1:2 :
  number of items to replace is not a multiple of replacement length

Is this intended by design or is it a bug that should be reported?

/Henrik



More information about the R-devel mailing list