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

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Henrik Bengtsson-3
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: No error when assigning values to an "empty" vector/matrix/array

Hervé Pagès
Hi Henrik,

On 10/23/2014 08:10 PM, Henrik Bengtsson wrote:

> 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?

Since [<- supports truncating of the right value, why an exception
should be made when the left vector has length 0?

Also note that these warnings or errors are complaining that the number
of items to replace (left length) is not a multiple of replacement
length (right length). This suggests that when the left length is a
multiple of the right length, everything is fine.
And this is actually the case when the left length is 0. Because
0 is a multiple of anything. So in that case, the right value is
truncated to length 0 and no warning is issued. Makes sense to me.

Cheers,
H.

>
> /Henrik
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: No error when assigning values to an "empty" vector/matrix/array

Henrik Bengtsson-3
On Oct 24, 2014 1:59 AM, "Hervé Pagès" <[hidden email]> wrote:

>
> Hi Henrik,
>
>
> On 10/23/2014 08:10 PM, Henrik Bengtsson wrote:
>>
>> 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?
>
>
> Since [<- supports truncating of the right value, why an exception
> should be made when the left vector has length 0?
>
> Also note that these warnings or errors are complaining that the number
> of items to replace (left length) is not a multiple of replacement
> length (right length). This suggests that when the left length is a
> multiple of the right length, everything is fine.
> And this is actually the case when the left length is 0. Because
> 0 is a multiple of anything. So in that case, the right value is
> truncated to length 0 and no warning is issued. Makes sense to me.

Thanks Hervé, you gave the perfect explanation/rationale for this being
consistent.

Henrik

PS. The background to my question was that I had a function that populated
a zero-row matrix column by column with values. These values were in turn
generated by another function that incorrectly read all values available in
for when indeed requestion zero (treating NULL ["read all"] and integer(0)
["read none"] equally). An implementation error that indeed gave the
correct value in the end, although in an extremely inefficient way.

>
> Cheers,
> H.
>
>>
>> /Henrik
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> --
> Hervé Pagès
>
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M1-B514
> P.O. Box 19024
> Seattle, WA 98109-1024
>
> E-mail: [hidden email]
> Phone:  (206) 667-5791
> Fax:    (206) 667-1319

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: No error when assigning values to an "empty" vector/matrix/array

S Ellison-2
> > Also note that these warnings or errors are complaining that the
> > number of items to replace (left length) is not a multiple of
> > replacement length (right length). This suggests that when the left
> > length is a multiple of the right length, everything is fine.
> > And this is actually the case when the left length is 0. Because
> > 0 is a multiple of anything. So in that case, the right value is
> > truncated to length 0 and no warning is issued. Makes sense to me.
>
> Thanks Hervé, you gave the perfect explanation/rationale for this being
> consistent.

This explains why a check for exact multiple of replacement length does not trigger a warning, but surely that is not sensible in the length 0 case. In all other cases, this check warns when there will be truncation of the replacement, and that seems to me the sensible intent of the check. A silent truncation to nothing is surely not the intended behaviour.
I can't help feeling that the 'check for multiple of length' was a neat portmanteau check for several possible problems when recycling is allowed, but that the possibility of assigning to a length 0 object was not considered.

I'd suggest logging it as an issue to for R-core to at least look at and either to fix or to at least warn of in documentation.

S Ellison




*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If
you have received this message in error, please notify the sender
immediately via +44(0)20 8943 7000 or notify [hidden email]
and delete this message and any copies from your computer and network.
LGC Limited. Registered in England 2991879.
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: No error when assigning values to an "empty" vector/matrix/array

Hervé Pagès
Hi,

On 10/24/2014 06:58 AM, S Ellison wrote:

>>> Also note that these warnings or errors are complaining that the
>>> number of items to replace (left length) is not a multiple of
>>> replacement length (right length). This suggests that when the left
>>> length is a multiple of the right length, everything is fine.
>>> And this is actually the case when the left length is 0. Because
>>> 0 is a multiple of anything. So in that case, the right value is
>>> truncated to length 0 and no warning is issued. Makes sense to me.
>>
>> Thanks Hervé, you gave the perfect explanation/rationale for this being
>> consistent.
>
> This explains why a check for exact multiple of replacement length does not trigger a warning, but surely that is not sensible in the length 0 case. In all other cases, this check warns when there will be truncation of the replacement, and that seems to me the sensible intent of the check. A silent truncation to nothing is surely not the intended behaviour.

Yes truncation should not be silent. But truncation to length zero
should not be seen as a special case either. What would be more
sensible is that [<- recognizes the 2 distinct situations that
deserve a warning:
   1. Truncation (i.e. when left length is < right length).
   2. Left length is > right length AND left length is not a multiple
      of right length.
Then the warning we get should be clear about which situation was
detected. So we would get a sensible warning all the time, even
when left length is 0.

H.

> I can't help feeling that the 'check for multiple of length' was a neat portmanteau check for several possible problems when recycling is allowed, but that the possibility of assigning to a length 0 object was not considered.
>
> I'd suggest logging it as an issue to for R-core to at least look at and either to fix or to at least warn of in documentation.
>
> S Ellison
>
>
>
>
> *******************************************************************
> This email and any attachments are confidential. Any u...{{dropped:26}}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel