

Hello,
Maybe I am not understanding how negative indexing works but
1) This is right.
(1:10)[1]
#[1] 2 3 4 5 6 7 8 9 10
2) Are these right? They are at least surprising to me.
(1:10)[0]
#integer(0)
(1:10)[seq_len(0)]
#integer(0)
It was the last example that made me ask, seq_len(0) whould avoid an
if/else or something similar.
Thanks in advance,
Rui Barradas
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rdevel


El sáb., 4 ago. 2018 a las 15:32, Rui Barradas
(< [hidden email]>) escribió:
>
> Hello,
>
> Maybe I am not understanding how negative indexing works but
>
> 1) This is right.
>
> (1:10)[1]
> #[1] 2 3 4 5 6 7 8 9 10
>
> 2) Are these right? They are at least surprising to me.
>
> (1:10)[0]
> #integer(0)
>
> (1:10)[seq_len(0)]
> #integer(0)
>
>
> It was the last example that made me ask, seq_len(0) whould avoid an
> if/else or something similar.
I think it's ok, because there is no negative zero integer, so 0 is 0.
1.0/0L # Inf
1.0/0.0 #  Inf
And the same can be said for integer(0), which is the result of
seq_len(0): there is no negative empty integer.
Iñaki
>
>
> Thanks in advance,
>
> Rui Barradas
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rdevel______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rdevel


Às 15:51 de 04/08/2018, Iñaki Úcar escreveu:
> El sáb., 4 ago. 2018 a las 15:32, Rui Barradas
> (< [hidden email]>) escribió:
>>
>> Hello,
>>
>> Maybe I am not understanding how negative indexing works but
>>
>> 1) This is right.
>>
>> (1:10)[1]
>> #[1] 2 3 4 5 6 7 8 9 10
>>
>> 2) Are these right? They are at least surprising to me.
>>
>> (1:10)[0]
>> #integer(0)
>>
>> (1:10)[seq_len(0)]
>> #integer(0)
>>
>>
>> It was the last example that made me ask, seq_len(0) whould avoid an
>> if/else or something similar.
>
> I think it's ok, because there is no negative zero integer, so 0 is 0.
Ok, this makes sense, I should have thought about that.
>
> 1.0/0L # Inf
> 1.0/0.0 #  Inf
>
> And the same can be said for integer(0), which is the result of
> seq_len(0): there is no negative empty integer.
I'm not completely convinced about this one, though.
I would expect seq_len(n) to remove the first n elements from the
vector, therefore, when n == 0, it would remove none.
And integer(0) is not the same as 0.
(1:10)[0] == (1:10)[0] == integer(0) # empty
(1:10)[seq_len(0)] == (1:10)[integer(0)]
And I have just reminded myself to run
identical(integer(0), integer(0))
It returns TRUE so my intuition is wrong, R is right.
End of story.
Thanks for the help,
Rui Barradas
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rdevel


This should more clearly illustrate the issue:
c(1, 2, 3, 4)[seq_len(4)]
#> numeric(0)
c(1, 2, 3, 4)[seq_len(3)]
#> [1] 4
c(1, 2, 3, 4)[seq_len(2)]
#> [1] 3 4
c(1, 2, 3, 4)[seq_len(1)]
#> [1] 2 3 4
c(1, 2, 3, 4)[seq_len(0)]
#> numeric(0)
Created on 20180805 by the reprex package (v0.2.0.9000).
On Sun, Aug 5, 2018 at 3:58 AM Rui Barradas < [hidden email]> wrote:
>
>
> Às 15:51 de 04/08/2018, Iñaki Úcar escreveu:
> > El sáb., 4 ago. 2018 a las 15:32, Rui Barradas
> > (< [hidden email]>) escribió:
> >>
> >> Hello,
> >>
> >> Maybe I am not understanding how negative indexing works but
> >>
> >> 1) This is right.
> >>
> >> (1:10)[1]
> >> #[1] 2 3 4 5 6 7 8 9 10
> >>
> >> 2) Are these right? They are at least surprising to me.
> >>
> >> (1:10)[0]
> >> #integer(0)
> >>
> >> (1:10)[seq_len(0)]
> >> #integer(0)
> >>
> >>
> >> It was the last example that made me ask, seq_len(0) whould avoid an
> >> if/else or something similar.
> >
> > I think it's ok, because there is no negative zero integer, so 0 is 0.
>
> Ok, this makes sense, I should have thought about that.
>
> >
> > 1.0/0L # Inf
> > 1.0/0.0 #  Inf
> >
> > And the same can be said for integer(0), which is the result of
> > seq_len(0): there is no negative empty integer.
>
> I'm not completely convinced about this one, though.
> I would expect seq_len(n) to remove the first n elements from the
> vector, therefore, when n == 0, it would remove none.
>
> And integer(0) is not the same as 0.
>
> (1:10)[0] == (1:10)[0] == integer(0) # empty
>
> (1:10)[seq_len(0)] == (1:10)[integer(0)]
>
>
> And I have just reminded myself to run
>
> identical(integer(0), integer(0))
>
> It returns TRUE so my intuition is wrong, R is right.
> End of story.
>
> Thanks for the help,
>
> Rui Barradas
>
> >
> > Iñaki
> >
> >>
> >>
> >> Thanks in advance,
> >>
> >> Rui Barradas
> >>
> >> ______________________________________________
> >> [hidden email] mailing list
> >> https://stat.ethz.ch/mailman/listinfo/rdevel>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rdevel>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rdevel


Thanks.
This is exactly the doubt I had.
Rui Barradas
Às 05:26 de 05/08/2018, Kenny Bell escreveu:
> This should more clearly illustrate the issue:
>
> c(1, 2, 3, 4)[seq_len(4)]
> #> numeric(0)
> c(1, 2, 3, 4)[seq_len(3)]
> #> [1] 4
> c(1, 2, 3, 4)[seq_len(2)]
> #> [1] 3 4
> c(1, 2, 3, 4)[seq_len(1)]
> #> [1] 2 3 4
> c(1, 2, 3, 4)[seq_len(0)]
> #> numeric(0)
> Created on 20180805 by the reprex package (v0.2.0.9000).
>
> On Sun, Aug 5, 2018 at 3:58 AM Rui Barradas < [hidden email]
> <mailto: [hidden email]>> wrote:
>
>
>
> Às 15:51 de 04/08/2018, Iñaki Úcar escreveu:
> > El sáb., 4 ago. 2018 a las 15:32, Rui Barradas
> > (< [hidden email] <mailto: [hidden email]>>) escribió:
> >>
> >> Hello,
> >>
> >> Maybe I am not understanding how negative indexing works but
> >>
> >> 1) This is right.
> >>
> >> (1:10)[1]
> >> #[1] 2 3 4 5 6 7 8 9 10
> >>
> >> 2) Are these right? They are at least surprising to me.
> >>
> >> (1:10)[0]
> >> #integer(0)
> >>
> >> (1:10)[seq_len(0)]
> >> #integer(0)
> >>
> >>
> >> It was the last example that made me ask, seq_len(0) whould avoid an
> >> if/else or something similar.
> >
> > I think it's ok, because there is no negative zero integer, so 0
> is 0.
>
> Ok, this makes sense, I should have thought about that.
>
> >
> > 1.0/0L # Inf
> > 1.0/0.0 #  Inf
> >
> > And the same can be said for integer(0), which is the result of
> > seq_len(0): there is no negative empty integer.
>
> I'm not completely convinced about this one, though.
> I would expect seq_len(n) to remove the first n elements from the
> vector, therefore, when n == 0, it would remove none.
>
> And integer(0) is not the same as 0.
>
> (1:10)[0] == (1:10)[0] == integer(0) # empty
>
> (1:10)[seq_len(0)] == (1:10)[integer(0)]
>
>
> And I have just reminded myself to run
>
> identical(integer(0), integer(0))
>
> It returns TRUE so my intuition is wrong, R is right.
> End of story.
>
> Thanks for the help,
>
> Rui Barradas
>
> >
> > Iñaki
> >
> >>
> >>
> >> Thanks in advance,
> >>
> >> Rui Barradas
> >>
> >> ______________________________________________
> >> [hidden email] <mailto: [hidden email]> mailing list
> >> https://stat.ethz.ch/mailman/listinfo/rdevel>
> ______________________________________________
> [hidden email] <mailto: [hidden email]> mailing list
> https://stat.ethz.ch/mailman/listinfo/rdevel>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rdevel


This is Circle 8..1.13 of the R Inferno.
On 05/08/2018 06:57, Rui Barradas wrote:
> Thanks.
> This is exactly the doubt I had.
>
> Rui Barradas
>
> Às 05:26 de 05/08/2018, Kenny Bell escreveu:
>> This should more clearly illustrate the issue:
>>
>> c(1, 2, 3, 4)[seq_len(4)]
>> #> numeric(0)
>> c(1, 2, 3, 4)[seq_len(3)]
>> #> [1] 4
>> c(1, 2, 3, 4)[seq_len(2)]
>> #> [1] 3 4
>> c(1, 2, 3, 4)[seq_len(1)]
>> #> [1] 2 3 4
>> c(1, 2, 3, 4)[seq_len(0)]
>> #> numeric(0)
>> Created on 20180805 by the reprex package (v0.2.0.9000).
>>
>> On Sun, Aug 5, 2018 at 3:58 AM Rui Barradas < [hidden email]
>> <mailto: [hidden email]>> wrote:
>>
>>
>>
>> Às 15:51 de 04/08/2018, Iñaki Úcar escreveu:
>> > El sáb., 4 ago. 2018 a las 15:32, Rui Barradas
>> > (< [hidden email] <mailto: [hidden email]>>) escribió:
>> >>
>> >> Hello,
>> >>
>> >> Maybe I am not understanding how negative indexing works but
>> >>
>> >> 1) This is right.
>> >>
>> >> (1:10)[1]
>> >> #[1] 2 3 4 5 6 7 8 9 10
>> >>
>> >> 2) Are these right? They are at least surprising to me.
>> >>
>> >> (1:10)[0]
>> >> #integer(0)
>> >>
>> >> (1:10)[seq_len(0)]
>> >> #integer(0)
>> >>
>> >>
>> >> It was the last example that made me ask, seq_len(0) whould
>> avoid an
>> >> if/else or something similar.
>> >
>> > I think it's ok, because there is no negative zero integer, so 0
>> is 0.
>>
>> Ok, this makes sense, I should have thought about that.
>>
>> >
>> > 1.0/0L # Inf
>> > 1.0/0.0 #  Inf
>> >
>> > And the same can be said for integer(0), which is the result of
>> > seq_len(0): there is no negative empty integer.
>>
>> I'm not completely convinced about this one, though.
>> I would expect seq_len(n) to remove the first n elements from the
>> vector, therefore, when n == 0, it would remove none.
>>
>> And integer(0) is not the same as 0.
>>
>> (1:10)[0] == (1:10)[0] == integer(0) # empty
>>
>> (1:10)[seq_len(0)] == (1:10)[integer(0)]
>>
>>
>> And I have just reminded myself to run
>>
>> identical(integer(0), integer(0))
>>
>> It returns TRUE so my intuition is wrong, R is right.
>> End of story.
>>
>> Thanks for the help,
>>
>> Rui Barradas
>>
>> >
>> > Iñaki
>> >
>> >>
>> >>
>> >> Thanks in advance,
>> >>
>> >> Rui Barradas
>> >>
>> >> ______________________________________________
>> >> [hidden email] <mailto: [hidden email]> mailing list
>> >> https://stat.ethz.ch/mailman/listinfo/rdevel>>
>> ______________________________________________
>> [hidden email] <mailto: [hidden email]> mailing list
>> https://stat.ethz.ch/mailman/listinfo/rdevel>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rdevel>

Patrick Burns
[hidden email]
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/bloghttp://www.burnsstat.com(home of:
'Impatient R'
'The R Inferno'
'Tao Te Programming')
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rdevel


Thanks,
This is what I needed.
I had read the R Inferno a long time ago and apparently forgot this one.
Rui Barradas
Às 08:46 de 05/08/2018, Patrick Burns escreveu:
> This is Circle 8..1.13 of the R Inferno.
>
>
> On 05/08/2018 06:57, Rui Barradas wrote:
>> Thanks.
>> This is exactly the doubt I had.
>>
>> Rui Barradas
>>
>> Às 05:26 de 05/08/2018, Kenny Bell escreveu:
>>> This should more clearly illustrate the issue:
>>>
>>> c(1, 2, 3, 4)[seq_len(4)]
>>> #> numeric(0)
>>> c(1, 2, 3, 4)[seq_len(3)]
>>> #> [1] 4
>>> c(1, 2, 3, 4)[seq_len(2)]
>>> #> [1] 3 4
>>> c(1, 2, 3, 4)[seq_len(1)]
>>> #> [1] 2 3 4
>>> c(1, 2, 3, 4)[seq_len(0)]
>>> #> numeric(0)
>>> Created on 20180805 by the reprex package (v0.2.0.9000).
>>>
>>> On Sun, Aug 5, 2018 at 3:58 AM Rui Barradas < [hidden email]
>>> <mailto: [hidden email]>> wrote:
>>>
>>>
>>>
>>> Às 15:51 de 04/08/2018, Iñaki Úcar escreveu:
>>> > El sáb., 4 ago. 2018 a las 15:32, Rui Barradas
>>> > (< [hidden email] <mailto: [hidden email]>>) escribió:
>>> >>
>>> >> Hello,
>>> >>
>>> >> Maybe I am not understanding how negative indexing works but
>>> >>
>>> >> 1) This is right.
>>> >>
>>> >> (1:10)[1]
>>> >> #[1] 2 3 4 5 6 7 8 9 10
>>> >>
>>> >> 2) Are these right? They are at least surprising to me.
>>> >>
>>> >> (1:10)[0]
>>> >> #integer(0)
>>> >>
>>> >> (1:10)[seq_len(0)]
>>> >> #integer(0)
>>> >>
>>> >>
>>> >> It was the last example that made me ask, seq_len(0) whould
>>> avoid an
>>> >> if/else or something similar.
>>> >
>>> > I think it's ok, because there is no negative zero integer, so 0
>>> is 0.
>>>
>>> Ok, this makes sense, I should have thought about that.
>>>
>>> >
>>> > 1.0/0L # Inf
>>> > 1.0/0.0 #  Inf
>>> >
>>> > And the same can be said for integer(0), which is the result of
>>> > seq_len(0): there is no negative empty integer.
>>>
>>> I'm not completely convinced about this one, though.
>>> I would expect seq_len(n) to remove the first n elements from the
>>> vector, therefore, when n == 0, it would remove none.
>>>
>>> And integer(0) is not the same as 0.
>>>
>>> (1:10)[0] == (1:10)[0] == integer(0) # empty
>>>
>>> (1:10)[seq_len(0)] == (1:10)[integer(0)]
>>>
>>>
>>> And I have just reminded myself to run
>>>
>>> identical(integer(0), integer(0))
>>>
>>> It returns TRUE so my intuition is wrong, R is right.
>>> End of story.
>>>
>>> Thanks for the help,
>>>
>>> Rui Barradas
>>>
>>> >
>>> > Iñaki
>>> >
>>> >>
>>> >>
>>> >> Thanks in advance,
>>> >>
>>> >> Rui Barradas
>>> >>
>>> >> ______________________________________________
>>> >> [hidden email] <mailto: [hidden email]> mailing
>>> list
>>> >> https://stat.ethz.ch/mailman/listinfo/rdevel>>>
>>> ______________________________________________
>>> [hidden email] <mailto: [hidden email]> mailing list
>>> https://stat.ethz.ch/mailman/listinfo/rdevel>>>
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rdevel>>
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rdevel


El dom., 5 ago. 2018 a las 6:27, Kenny Bell (< [hidden email]>) escribió:
>
> This should more clearly illustrate the issue:
>
> c(1, 2, 3, 4)[seq_len(4)]
> #> numeric(0)
> c(1, 2, 3, 4)[seq_len(3)]
> #> [1] 4
> c(1, 2, 3, 4)[seq_len(2)]
> #> [1] 3 4
> c(1, 2, 3, 4)[seq_len(1)]
> #> [1] 2 3 4
> c(1, 2, 3, 4)[seq_len(0)]
> #> numeric(0)
> Created on 20180805 by the reprex package (v0.2.0.9000).
IMO, the problem is that you are reading it sequentially: "" remove
"seq_" a sequence "len(0)" of length zero. But that's not how R works
(how programming languages work in general). Instead, the sequence is
evaluated in the first place, and then the sign may apply as long as
you provided something that can hold a sign. And an empty element has
no sign, so the sign is lost.
Iñaki
>
> On Sun, Aug 5, 2018 at 3:58 AM Rui Barradas < [hidden email]> wrote:
>>
>>
>>
>> Às 15:51 de 04/08/2018, Iñaki Úcar escreveu:
>> > El sáb., 4 ago. 2018 a las 15:32, Rui Barradas
>> > (< [hidden email]>) escribió:
>> >>
>> >> Hello,
>> >>
>> >> Maybe I am not understanding how negative indexing works but
>> >>
>> >> 1) This is right.
>> >>
>> >> (1:10)[1]
>> >> #[1] 2 3 4 5 6 7 8 9 10
>> >>
>> >> 2) Are these right? They are at least surprising to me.
>> >>
>> >> (1:10)[0]
>> >> #integer(0)
>> >>
>> >> (1:10)[seq_len(0)]
>> >> #integer(0)
>> >>
>> >>
>> >> It was the last example that made me ask, seq_len(0) whould avoid an
>> >> if/else or something similar.
>> >
>> > I think it's ok, because there is no negative zero integer, so 0 is 0.
>>
>> Ok, this makes sense, I should have thought about that.
>>
>> >
>> > 1.0/0L # Inf
>> > 1.0/0.0 #  Inf
>> >
>> > And the same can be said for integer(0), which is the result of
>> > seq_len(0): there is no negative empty integer.
>>
>> I'm not completely convinced about this one, though.
>> I would expect seq_len(n) to remove the first n elements from the
>> vector, therefore, when n == 0, it would remove none.
>>
>> And integer(0) is not the same as 0.
>>
>> (1:10)[0] == (1:10)[0] == integer(0) # empty
>>
>> (1:10)[seq_len(0)] == (1:10)[integer(0)]
>>
>>
>> And I have just reminded myself to run
>>
>> identical(integer(0), integer(0))
>>
>> It returns TRUE so my intuition is wrong, R is right.
>> End of story.
>>
>> Thanks for the help,
>>
>> Rui Barradas
>>
>> >
>> > Iñaki
>> >
>> >>
>> >>
>> >> Thanks in advance,
>> >>
>> >> Rui Barradas
>> >>
>> >> ______________________________________________
>> >> [hidden email] mailing list
>> >> https://stat.ethz.ch/mailman/listinfo/rdevel>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rdevel______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rdevel


FYI, this behavior is documented in Section 3.4.1 'Indexing by
vectors' of 'R Language Definition' (accessible for instance via
help.start()):
"*Integer* [...] A special case is the zero index, which has null
effects: x[0] is an empty vector and otherwise including zeros among
positive or negative indices has the same effect as if they were
omitted."
The rest of that section is very useful and well written. I used it as
the goto reference to implement support for all those indexing
alternatives in matrixStats.
/Henrik
On Sun, Aug 5, 2018 at 3:42 AM Iñaki Úcar < [hidden email]> wrote:
>
> El dom., 5 ago. 2018 a las 6:27, Kenny Bell (< [hidden email]>) escribió:
> >
> > This should more clearly illustrate the issue:
> >
> > c(1, 2, 3, 4)[seq_len(4)]
> > #> numeric(0)
> > c(1, 2, 3, 4)[seq_len(3)]
> > #> [1] 4
> > c(1, 2, 3, 4)[seq_len(2)]
> > #> [1] 3 4
> > c(1, 2, 3, 4)[seq_len(1)]
> > #> [1] 2 3 4
> > c(1, 2, 3, 4)[seq_len(0)]
> > #> numeric(0)
> > Created on 20180805 by the reprex package (v0.2.0.9000).
>
> IMO, the problem is that you are reading it sequentially: "" remove
> "seq_" a sequence "len(0)" of length zero. But that's not how R works
> (how programming languages work in general). Instead, the sequence is
> evaluated in the first place, and then the sign may apply as long as
> you provided something that can hold a sign. And an empty element has
> no sign, so the sign is lost.
>
> Iñaki
>
> >
> > On Sun, Aug 5, 2018 at 3:58 AM Rui Barradas < [hidden email]> wrote:
> >>
> >>
> >>
> >> Às 15:51 de 04/08/2018, Iñaki Úcar escreveu:
> >> > El sáb., 4 ago. 2018 a las 15:32, Rui Barradas
> >> > (< [hidden email]>) escribió:
> >> >>
> >> >> Hello,
> >> >>
> >> >> Maybe I am not understanding how negative indexing works but
> >> >>
> >> >> 1) This is right.
> >> >>
> >> >> (1:10)[1]
> >> >> #[1] 2 3 4 5 6 7 8 9 10
> >> >>
> >> >> 2) Are these right? They are at least surprising to me.
> >> >>
> >> >> (1:10)[0]
> >> >> #integer(0)
> >> >>
> >> >> (1:10)[seq_len(0)]
> >> >> #integer(0)
> >> >>
> >> >>
> >> >> It was the last example that made me ask, seq_len(0) whould avoid an
> >> >> if/else or something similar.
> >> >
> >> > I think it's ok, because there is no negative zero integer, so 0 is 0.
> >>
> >> Ok, this makes sense, I should have thought about that.
> >>
> >> >
> >> > 1.0/0L # Inf
> >> > 1.0/0.0 #  Inf
> >> >
> >> > And the same can be said for integer(0), which is the result of
> >> > seq_len(0): there is no negative empty integer.
> >>
> >> I'm not completely convinced about this one, though.
> >> I would expect seq_len(n) to remove the first n elements from the
> >> vector, therefore, when n == 0, it would remove none.
> >>
> >> And integer(0) is not the same as 0.
> >>
> >> (1:10)[0] == (1:10)[0] == integer(0) # empty
> >>
> >> (1:10)[seq_len(0)] == (1:10)[integer(0)]
> >>
> >>
> >> And I have just reminded myself to run
> >>
> >> identical(integer(0), integer(0))
> >>
> >> It returns TRUE so my intuition is wrong, R is right.
> >> End of story.
> >>
> >> Thanks for the help,
> >>
> >> Rui Barradas
> >>
> >> >
> >> > Iñaki
> >> >
> >> >>
> >> >>
> >> >> Thanks in advance,
> >> >>
> >> >> Rui Barradas
> >> >>
> >> >> ______________________________________________
> >> >> [hidden email] mailing list
> >> >> https://stat.ethz.ch/mailman/listinfo/rdevel> >>
> >> ______________________________________________
> >> [hidden email] mailing list
> >> https://stat.ethz.ch/mailman/listinfo/rdevel>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rdevel______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rdevel


Hello,
Thanks for the pointer.
Inline.
On 29/08/2018 04:17, Henrik Bengtsson wrote:
> FYI, this behavior is documented in Section 3.4.1 'Indexing by
> vectors' of 'R Language Definition' (accessible for instance via
> help.start()):
>
> "*Integer* [...] A special case is the zero index, which has null
> effects: x[0] is an empty vector and otherwise including zeros among
> positive or negative indices has the same effect as if they were
> omitted."
>
So I was in part right, the zero index is handled as a special case.
My use case was an operation in a function. I wasn't testing whether the
result was of length zero, I was just using seq_len(result) to avoid the
test. And found the error surprising.
Thanks again,
Rui Barradas
> The rest of that section is very useful and well written. I used it as
> the goto reference to implement support for all those indexing
> alternatives in matrixStats.
>
> /Henrik
> On Sun, Aug 5, 2018 at 3:42 AM Iñaki Úcar < [hidden email]> wrote:
>>
>> El dom., 5 ago. 2018 a las 6:27, Kenny Bell (< [hidden email]>) escribió:
>>>
>>> This should more clearly illustrate the issue:
>>>
>>> c(1, 2, 3, 4)[seq_len(4)]
>>> #> numeric(0)
>>> c(1, 2, 3, 4)[seq_len(3)]
>>> #> [1] 4
>>> c(1, 2, 3, 4)[seq_len(2)]
>>> #> [1] 3 4
>>> c(1, 2, 3, 4)[seq_len(1)]
>>> #> [1] 2 3 4
>>> c(1, 2, 3, 4)[seq_len(0)]
>>> #> numeric(0)
>>> Created on 20180805 by the reprex package (v0.2.0.9000).
>>
>> IMO, the problem is that you are reading it sequentially: "" remove
>> "seq_" a sequence "len(0)" of length zero. But that's not how R works
>> (how programming languages work in general). Instead, the sequence is
>> evaluated in the first place, and then the sign may apply as long as
>> you provided something that can hold a sign. And an empty element has
>> no sign, so the sign is lost.
>>
>> Iñaki
>>
>>>
>>> On Sun, Aug 5, 2018 at 3:58 AM Rui Barradas < [hidden email]> wrote:
>>>>
>>>>
>>>>
>>>> Às 15:51 de 04/08/2018, Iñaki Úcar escreveu:
>>>>> El sáb., 4 ago. 2018 a las 15:32, Rui Barradas
>>>>> (< [hidden email]>) escribió:
>>>>>>
>>>>>> Hello,
>>>>>>
>>>>>> Maybe I am not understanding how negative indexing works but
>>>>>>
>>>>>> 1) This is right.
>>>>>>
>>>>>> (1:10)[1]
>>>>>> #[1] 2 3 4 5 6 7 8 9 10
>>>>>>
>>>>>> 2) Are these right? They are at least surprising to me.
>>>>>>
>>>>>> (1:10)[0]
>>>>>> #integer(0)
>>>>>>
>>>>>> (1:10)[seq_len(0)]
>>>>>> #integer(0)
>>>>>>
>>>>>>
>>>>>> It was the last example that made me ask, seq_len(0) whould avoid an
>>>>>> if/else or something similar.
>>>>>
>>>>> I think it's ok, because there is no negative zero integer, so 0 is 0.
>>>>
>>>> Ok, this makes sense, I should have thought about that.
>>>>
>>>>>
>>>>> 1.0/0L # Inf
>>>>> 1.0/0.0 #  Inf
>>>>>
>>>>> And the same can be said for integer(0), which is the result of
>>>>> seq_len(0): there is no negative empty integer.
>>>>
>>>> I'm not completely convinced about this one, though.
>>>> I would expect seq_len(n) to remove the first n elements from the
>>>> vector, therefore, when n == 0, it would remove none.
>>>>
>>>> And integer(0) is not the same as 0.
>>>>
>>>> (1:10)[0] == (1:10)[0] == integer(0) # empty
>>>>
>>>> (1:10)[seq_len(0)] == (1:10)[integer(0)]
>>>>
>>>>
>>>> And I have just reminded myself to run
>>>>
>>>> identical(integer(0), integer(0))
>>>>
>>>> It returns TRUE so my intuition is wrong, R is right.
>>>> End of story.
>>>>
>>>> Thanks for the help,
>>>>
>>>> Rui Barradas
>>>>
>>>>>
>>>>> Iñaki
>>>>>
>>>>>>
>>>>>>
>>>>>> Thanks in advance,
>>>>>>
>>>>>> Rui Barradas
>>>>>>
>>>>>> ______________________________________________
>>>>>> [hidden email] mailing list
>>>>>> https://stat.ethz.ch/mailman/listinfo/rdevel>>>>
>>>> ______________________________________________
>>>> [hidden email] mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/rdevel>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rdevel>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rdevel>

This email has been checked for viruses by AVG.
https://www.avg.com______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rdevel

