class of seq(length=n) values (was Re: [R] Zero Index Origin?)
Matthias Burger
ml-r-help at epigenomics.com
Tue Apr 13 14:14:52 CEST 2004
Dear Brian Ripley,
following your advice (very much appreciated) of using seq(length=n) instead of
1:n constructs in loop constructions
(usually prepended by the not so elegant if (n > 0))
I was somewhat surprised to find that (in R 1.8.1 & R 1.9.0 beta (2004-03-29) on
Debian 3.0)
and using the methods package
carrel>a <- seq(1,5)
carrel>class(a)
[1] "integer"
carrel>class(seq(along=a))
[1] "integer"
but
carrel>class(seq(length=length(a)))
[1] "numeric"
and from ?seq
...
Value:
The result is of 'mode' '"integer"' if 'from' is (numerically
equal to an) integer and 'by' is not specified.
...
it is not (to me, of course) obvious that if length is specified and from
omitted a numeric result sequence is returned.
This might only matter in conjunction with S4 class slot assignments where the
integer class requirement is important.
OTOH a
for (i in as.integer(seq(length=length(a)))) { ... }
is not so elegant either.
Could you please enlighten me as to why this behaviour was chosen and if there
is any more elegant way than using
as.integer(seq(length=length(a)))
to get the desired result (given that I have to use a loop in the first place).
Regards,
Matthias
Prof Brian Ripley wrote:
> Much of R is itself written in R, so you cannot possibly change something
> as fundamental as this. Further, index 0 has a special meaning that you
> would lose if R have 0-based indexing.
>
> However, the R thinking is to work with whole objects (vectors, arrays,
> lists ...) and you rather rarely need to know what numbers are in an index
> vector. There are usages such as 1:n, and those are quite often wrong:
> they should be seq(length=n) or seq(along=x) or some such, since n might
> be zero. If you are writing code that works with single elements, you are
> probably a lot better off writing C code to link into R (and C is
> 0-based ...).
>
> On Wed, 31 Mar 2004, Bob Cain wrote:
>
>
>>I'm very new to R and utterly blown away by not only the
>>language but the unbelievable set of packages and the
>>documentation and the documentation standards and...
>>
>>I was an early APL user and never lost my love for it and in
>>R I find most of the essential things I loved about APL
>>except for one thing. At this early stage of my learning I
>>can't yet determine if there is a way to effect what in APL
>>was zero index origin, the ordinality of indexes starts with
>>0 instead of 1. Is it possible to effect that in R without
>>a lot of difficulty?
>>
>>I come here today from the world of DSP research and
>>development where Matlab has a near hegemony. I see no
>>reason whatsoever that R couldn't replace it with a _far_
>>better and _far_ less idiosyncratic framework. I'd be
>>interested in working on a Matlab equivalent DSP package for
>>R (if that isn't being done by someone) and one of the
>>things most criticized about Matlab from the standpoint of
>>the DSP programmer is its insistence on 1 origin indexing.
>>Any feedback greatly appreciated.
>>
>>
>>Thanks,
>>
>>Bob
>>
>
>
--
Matthias Burger
Bioinformatics R&D
Epigenomics AG www.epigenomics.com
Kleine Präsidentenstraße 1 fax: +49-30-24345-555
10178 Berlin Germany phone: +49-30-24345-0
More information about the R-help
mailing list