Skip to content

array(list(),c(2,5)) gives error in R 1.8.1

2 messages · Christoph Lehmann, Tony Plate

#
Hi

In R 1.7 the following worked fine:
[,1] [,2] [,3] [,4] [,5]
[1,] NULL NULL NULL NULL NULL
[2,] NULL NULL NULL NULL NULL

now in R 1.8.1 I get the error:

Error in rep.int(data, t1) : invalid number of copies in "rep"
In addition: Warning message:
NAs introduced by coercion

thanks for help, I need this possibility for storing objects (lm
results) in an array

cheers

Christoph
#
I confirmed this -- array(list(), c(2,2)) works in R 1.6.2 and R 1.7.1, but 
not in R 1.8.0.  This appears to be due to a change in array(): rep(data, 
t1) was changed to rep.int(data, t1).  When data=list(), t1==Inf, and 
rep(data, t1) returns list(), while rep.int(data, t1) gives an 
error.  Here's a transcript from R 1.8.0:

 > array
function (data = NA, dim = length(data), dimnames = NULL)
{
     data <- as.vector(data)
     vl <- prod(dim)
     if (length(data) != vl) {
         t1 <- ceiling(vl/length(data))
         data <- rep.int(data, t1)
         if (length(data) != vl)
             data <- data[1:vl]
     }
     if (length(dim))
         dim(data) <- dim
     if (is.list(dimnames) && length(dimnames))
         dimnames(data) <- dimnames
     data
}
<environment: namespace:base>
 > rep(list(), Inf)
list()
 > rep.int(list(), Inf)
Error in rep.int(list(), Inf) : invalid number of copies in "rep"
In addition: Warning message:
NAs introduced by coercion
 > array(numeric(3), 0,0)
numeric(0)
 >

There's also the dangerous construct data[1:v1] in array() 
(data[seq(len=v1)] would be much safer).  However, it appears that the 1:0 
trap doesn't occur under normal circumstances (because if v1=0, then t1 
will be either 0 or Inf, and length(rep.int(data, t1)) will be 0 or an 
error will have occurred (with most common data types at 
least).  However^2, given that functions in R don't always produce the 
results one might expect, it might be safer to change this to 
data[seq(len=v1)].

A workaround is to give array() a data value of the correct length:

 > array(list()[1:4], c(2,2))
      [,1] [,2]
[1,] NULL NULL
[2,] NULL NULL
 >

-- Tony Plate
At Wednesday 10:35 AM 1/14/2004 +0100, you wrote: