

Hi,
This may belong more to rdevelop, but general discussion may be useful
(for the how manyth time ?)
seq(2,5,2)
seq(5,2,2)
both result in
Error in seq.default(2, 5, 2) : wrong sign in 'by' argument
But often, if not always, mathematicians and programmers want a
behaviour e.g. in for loops, where this statement results in an empty
statement, that is
for (ii in seq(2,5,2)) print(ii)
were equivalent to
for (ii in NULL) print(ii).
The relevant part in seq.default is now
if (n < 0)
stop("wrong sign in 'by' argument")
but could be changed by option to
return(NULL)
I think there should be an option to seq requiring this behaviour, or a
specific function, may be even a special operator, e.g. %;%:
3;5 resulting in NULL.
What do you think?
Christian

Dr. Christian W. Hoffmann,
Swiss Federal Research Institute WSL
Mathematics + Statistical Computing
Zuercherstrasse 111
CH8903 Birmensdorf, Switzerland
Tel +41447392277 (office) 111(exchange)
Fax +41447392215 (fax)
[hidden email]
http://www.wsl.ch/staff/christian.hoffmannInternational Conference 5.7.6.2006 Ekaterinburg Russia
"Climate changes and their impact on boreal and temperate forests"
http://ecoinf.uran.ru/conference/______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


On 3/27/2006 4:41 AM, Christian Hoffmann wrote:
> Hi,
>
> This may belong more to rdevelop, but general discussion may be useful
> (for the how manyth time ?)
>
> seq(2,5,2)
> seq(5,2,2)
>
> both result in
>
> Error in seq.default(2, 5, 2) : wrong sign in 'by' argument
>
> But often, if not always, mathematicians and programmers want a
> behaviour e.g. in for loops, where this statement results in an empty
> statement, that is
>
> for (ii in seq(2,5,2)) print(ii)
>
> were equivalent to
>
> for (ii in NULL) print(ii).
>
> The relevant part in seq.default is now
>
> if (n < 0)
> stop("wrong sign in 'by' argument")
>
> but could be changed by option to
>
> return(NULL)
>
> I think there should be an option to seq requiring this behaviour, or a
> specific function, may be even a special operator, e.g. %;%:
>
> 3;5 resulting in NULL.
>
> What do you think?
If you want optional behaviour, the easiest way is to write your own
wrapper function. E.g.
emptyseq < function(from, to, by) {
if ((tofrom)*by < 0) return(NULL)
else return(seq(from, to, by))
}
I don't think this is a desirable default, though. We already have a
special way to handle the most common case, i.e.
seq(1, length(x), 1)
should be written as
seq(along=x))
to handle the length(x) == 0 case the way you're requesting.
But I'm not so sure that seq(2,5,2) should really be NULL; it looks
much more like an error to me. You say mathematicians and programmers
want this behaviour, but I really can't think of any examples other than
the one above.
As a general principle, I think it's better to throw an error on
ambiguous or apparently erroneous code rather than always returning an
answer. If the code can be made unambiguous, it should be. (R doesn't
always follow this principle; for example, recycling of vectors of
lengths bigger than 1 is probably an error at least as often as it's
intended.)
Duncan Murdoch
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


You should be able to do it yourself; e.g.,
my.seq < function(...) if((to  from) * by < 0) NULL else seq(...)
and use that instead when you want that behavior.
Andy
From: Christian Hoffmann
>
> Hi,
>
> This may belong more to rdevelop, but general discussion may
> be useful
> (for the how manyth time ?)
>
> seq(2,5,2)
> seq(5,2,2)
>
> both result in
>
> Error in seq.default(2, 5, 2) : wrong sign in 'by' argument
>
> But often, if not always, mathematicians and programmers want a
> behaviour e.g. in for loops, where this statement results in an empty
> statement, that is
>
> for (ii in seq(2,5,2)) print(ii)
>
> were equivalent to
>
> for (ii in NULL) print(ii).
>
> The relevant part in seq.default is now
>
> if (n < 0)
> stop("wrong sign in 'by' argument")
>
> but could be changed by option to
>
> return(NULL)
>
> I think there should be an option to seq requiring this
> behaviour, or a
> specific function, may be even a special operator, e.g. %;%:
>
> 3;5 resulting in NULL.
>
> What do you think?
>
> Christian
> 
> Dr. Christian W. Hoffmann,
> Swiss Federal Research Institute WSL
> Mathematics + Statistical Computing
> Zuercherstrasse 111
> CH8903 Birmensdorf, Switzerland
>
> Tel +41447392277 (office) 111(exchange)
> Fax +41447392215 (fax)
> [hidden email] http://www.wsl.ch/staff/christian.hoffmann>
> International Conference 5.7.6.2006 Ekaterinburg Russia
> "Climate changes and their impact on boreal and temperate
> forests" http://ecoinf.uran.ru/conference/>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide!
> http://www.Rproject.org/postingguide.html>
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


On Mon, 27 Mar 2006, Christian Hoffmann wrote:
> Hi,
>
> This may belong more to rdevelop, but general discussion may be useful
> (for the how manyth time ?)
The place for general discussion of changes to R is the Rdevel list.
There is almost no scope to change things like this, as there is so much
existing code which relies on it (and it is also compatible with S).
> seq(2,5,2)
> seq(5,2,2)
>
> both result in
>
> Error in seq.default(2, 5, 2) : wrong sign in 'by' argument
>
> But often, if not always, mathematicians and programmers want a
> behaviour e.g. in for loops, where this statement results in an empty
> statement, that is
>
> for (ii in seq(2,5,2)) print(ii)
>
> were equivalent to
>
> for (ii in NULL) print(ii).
>
> The relevant part in seq.default is now
>
> if (n < 0)
> stop("wrong sign in 'by' argument")
>
> but could be changed by option to
>
> return(NULL)
Why is NULL plausible? I would think integer(0) is more likely, but if
you think this should not be an error, then another plausible
interpretation is the intersection of {2 ... 5} and {2, 0, 2, ...}, that
is {2}. (The only language I can think of with a close analogue is
Fortran DO loops, where DO 10 I=2,5,2 used to be {2} and was defined in
F77 to be empty, so I don't think the interpretation is unambiguous.)
[As an aside, one could argue that 'for (ii in NULL)' should be an error,
since the help page says
seq: An expression evaluating to a vector (including a list).
and NULL is not a vector.]
> I think there should be an option to seq requiring this behaviour, or a
> specific function, may be even a special operator, e.g. %;%:
>
> 3;5 resulting in NULL.
>
> What do you think?
It cannot be the default, and if you need it, why not write your own
function to do it? S has survived ca 18 years without it, so the need
cannot be overwhelming.

Brian D. Ripley, [hidden email]
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


Hi.
seq() is a complex beast indeed. 'by' being the wrong
sign is a special case of the behaviour seen in the
following code snippets, the first of which is correctly
rejected by seq(), the second of which should arguably
return a three element complex vector.
> seq(from=1,to=3,by=1+1i)
Error in n < 0 : invalid comparison with complex values
> seq(from=1,to=4+3i,by=1+1i)
Error in n < 0 : invalid comparison with complex values
best wishes
Robin
On 27 Mar 2006, at 13:23, Duncan Murdoch wrote:
> On 3/27/2006 4:41 AM, Christian Hoffmann wrote:
>> Hi,
>>
>> This may belong more to rdevelop, but general discussion may be
>> useful
>> (for the how manyth time ?)
>>
>> seq(2,5,2)
>> seq(5,2,2)
>>
>> both result in
>>
>> Error in seq.default(2, 5, 2) : wrong sign in 'by' argument
>>
>> But often, if not always, mathematicians and programmers want a
>> behaviour e.g. in for loops, where this statement results in an empty
>> statement, that is
>>
>> for (ii in seq(2,5,2)) print(ii)
>>
>> were equivalent to
>>
>> for (ii in NULL) print(ii).
>>
>> The relevant part in seq.default is now
>>
>> if (n < 0)
>> stop("wrong sign in 'by' argument")
>>
>> but could be changed by option to
>>
>> return(NULL)
>>
>> I think there should be an option to seq requiring this behaviour,
>> or a
>> specific function, may be even a special operator, e.g. %;%:
>>
>> 3;5 resulting in NULL.
>>
>> What do you think?
>
> If you want optional behaviour, the easiest way is to write your own
> wrapper function. E.g.
>
> emptyseq < function(from, to, by) {
> if ((tofrom)*by < 0) return(NULL)
> else return(seq(from, to, by))
> }
>
> I don't think this is a desirable default, though. We already have a
> special way to handle the most common case, i.e.
>
> seq(1, length(x), 1)
>
> should be written as
>
> seq(along=x))
>
> to handle the length(x) == 0 case the way you're requesting.
>
> But I'm not so sure that seq(2,5,2) should really be NULL; it looks
> much more like an error to me. You say mathematicians and programmers
> want this behaviour, but I really can't think of any examples other
> than
> the one above.
>
> As a general principle, I think it's better to throw an error on
> ambiguous or apparently erroneous code rather than always returning an
> answer. If the code can be made unambiguous, it should be. (R
> doesn't
> always follow this principle; for example, recycling of vectors of
> lengths bigger than 1 is probably an error at least as often as it's
> intended.)
>
> Duncan Murdoch
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide! http://www.Rproject.org/posting
> guide.html

Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 02380597743
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


On 3/27/2006 8:28 AM, Robin Hankin wrote:
> Hi.
>
> seq() is a complex beast indeed. 'by' being the wrong
> sign is a special case of the behaviour seen in the
> following code snippets, the first of which is correctly
> rejected by seq(), the second of which should arguably
> return a three element complex vector.
>
>
> > seq(from=1,to=3,by=1+1i)
> Error in n < 0 : invalid comparison with complex values
>
> > seq(from=1,to=4+3i,by=1+1i)
> Error in n < 0 : invalid comparison with complex values
I don't think seq() could reasonably be expected to handle "to" and "by"
arguments with complex values. Trying to divide the (tofrom)
difference by (by) to find how many steps to take would usually result
in enough rounding error that the result wouldn't be realvalued. It's
enough of a miracle that it correctly handles
seq(from=1, by=1+1i, len=4)
Duncan Murdoch
>
> best wishes
>
> Robin
>
>
>
> On 27 Mar 2006, at 13:23, Duncan Murdoch wrote:
>
>> On 3/27/2006 4:41 AM, Christian Hoffmann wrote:
>>> Hi,
>>>
>>> This may belong more to rdevelop, but general discussion may be
>>> useful
>>> (for the how manyth time ?)
>>>
>>> seq(2,5,2)
>>> seq(5,2,2)
>>>
>>> both result in
>>>
>>> Error in seq.default(2, 5, 2) : wrong sign in 'by' argument
>>>
>>> But often, if not always, mathematicians and programmers want a
>>> behaviour e.g. in for loops, where this statement results in an empty
>>> statement, that is
>>>
>>> for (ii in seq(2,5,2)) print(ii)
>>>
>>> were equivalent to
>>>
>>> for (ii in NULL) print(ii).
>>>
>>> The relevant part in seq.default is now
>>>
>>> if (n < 0)
>>> stop("wrong sign in 'by' argument")
>>>
>>> but could be changed by option to
>>>
>>> return(NULL)
>>>
>>> I think there should be an option to seq requiring this behaviour,
>>> or a
>>> specific function, may be even a special operator, e.g. %;%:
>>>
>>> 3;5 resulting in NULL.
>>>
>>> What do you think?
>>
>> If you want optional behaviour, the easiest way is to write your own
>> wrapper function. E.g.
>>
>> emptyseq < function(from, to, by) {
>> if ((tofrom)*by < 0) return(NULL)
>> else return(seq(from, to, by))
>> }
>>
>> I don't think this is a desirable default, though. We already have a
>> special way to handle the most common case, i.e.
>>
>> seq(1, length(x), 1)
>>
>> should be written as
>>
>> seq(along=x))
>>
>> to handle the length(x) == 0 case the way you're requesting.
>>
>> But I'm not so sure that seq(2,5,2) should really be NULL; it looks
>> much more like an error to me. You say mathematicians and programmers
>> want this behaviour, but I really can't think of any examples other
>> than
>> the one above.
>>
>> As a general principle, I think it's better to throw an error on
>> ambiguous or apparently erroneous code rather than always returning an
>> answer. If the code can be made unambiguous, it should be. (R
>> doesn't
>> always follow this principle; for example, recycling of vectors of
>> lengths bigger than 1 is probably an error at least as often as it's
>> intended.)
>>
>> Duncan Murdoch
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide! http://www.Rproject.org/posting
>> guide.html
>
> 
> Robin Hankin
> Uncertainty Analyst
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
> tel 02380597743
>
>
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


Hi Duncan et al
>
> I don't think seq() could reasonably be expected to handle "to" and
> "by" arguments with complex values. Trying to divide the (tofrom)
> difference by (by) to find how many steps to take would usually
> result in enough rounding error that the result wouldn't be real
> valued. It's enough of a miracle that it correctly handles
>
> seq(from=1, by=1+1i, len=4)
>
> Duncan Murdoch
>
well it depends on your definition of miracles, but I wouldn't say
1 + (0:3) * (1+1i)
is particularly miraculous ;\
best wishes
rksh

Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 02380597743
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html

