[R] bug or bizarre feature?

David Winsemius dwinsemius at comcast.net
Mon Nov 30 20:56:35 CET 2009


On Nov 30, 2009, at 2:24 PM, Henrique Dallazuanna wrote:

> Dim argument must be a integer, see the return of:

Less true than you might expect:

 > x <- 3.5
 > y <- 5.2
 > z <- array(0, dim=c(x,y))
 > z
      [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    0    0    0    0
[3,]    0    0    0    0    0

What happens is coercion toward zero of the argument, ... which may  
not be the same as the displayed value.

 > array
function (data = NA, dim = length(data), dimnames = NULL)
{
     data <- as.vector(data)
     dim <- as.integer(dim)
<snipped>

I was left wondering if as.integer(round(dim))) wouldn't be more what  
people would expect.


-- 
David.
>
> as.integer(slope*xdim)
>
> Try this:
>
> udim <- ceiling(max(slope*xdim,5))
>
> On Mon, Nov 30, 2009 at 5:15 PM, Rupert Mazzucco <rmaz at gmx.net> wrote:
>> Hello,
>>
>> I'm running into a very strange problem:
>>
>>> xrange <- c(-2.5,2.5)
>>> xdim <- 100
>>> mobility <- 0.1
>>> slope <- 1.16
>>> urange <- slope*xrange
>>>         udim <- max(slope*xdim,5)
>>>         du <- (urange[2]-urange[1])/udim
>>>         uvec <- urange[1]+(1:udim-0.5)*du
>>>         # type dependent weight function
>>>         ckern <- array(0,dim=c(udim,udim))
>>>         diag(ckern) = wfun(uvec,slope,mobility)
>> Error in `diag<-`(`*tmp*`, value = c(0.992300064325398,  
>> 0.990746129315703,  :
>>  replacement diagonal has wrong length
>>
>> It turns out that the array ckern has the wrong size for some reason.
>> Instead of 116x116, it is only 115x115.
>>
>>> udim
>> [1] 116
>>> length(uvec)
>> [1] 116
>>> length(wfun(uvec,slope,mobility))
>> [1] 116
>>> dim(ckern)
>> [1] 115 115
>>
>> The "taint" or whatever that is, is even transferable
>>
>>> n <- udim
>>> n
>> [1] 116
>>> ckern <- array(0,dim=c(n,n))
>>> dim(ckern)
>> [1] 115 115
>>> m <- n
>>> m
>> [1] 116
>>> ckern <- array(0,dim=c(m,m))
>>> dim(ckern)
>> [1] 115 115
>>
>> But when I set it explicitly, it does what it should:
>>
>>> n <- 116
>>> n
>> [1] 116
>>> ckern <- array(0,dim=c(n,n))
>>> dim(ckern)
>> [1] 116 116
>>
>> Note that the funny behavior seems to be peculiar to this one value  
>> of slope <- 1.16,
>> many others work fine, e.g.
>>
>>> slope <- 1.08
>>> urange <- slope*xrange
>>>         udim <- max(slope*xdim,5)
>>>         du <- (urange[2]-urange[1])/udim
>>>         uvec <- urange[1]+(1:udim-0.5)*du
>>>         # type dependent weight function
>>>         ckern <- array(0,dim=c(udim,udim))
>>>         diag(ckern) = wfun(uvec,slope,mobility)
>>> dim(ckern)
>> [1] 108 108
>>
>> This is R 2.10.0, but also happened in 2.8.0. Can anybody tell me  
>> what
>> is going on here, and how I can get my array to be the right size?
>>
>> Thanks,
>> Rupert
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> -- 
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list