Paul Johnson
2016-Aug-03 20:12 UTC
[Rd] seq.int does not return a sequence of integers sometimes
I have a script that goes wrong because I assumed that seq.int would return integers. Below please see it does not unless user is super cautious about inserting "L" with inputs. I think seq.int should do coercion for me before returning the sequence.> xx <- seq.int(1,10) > class(xx)[1] "integer"> is.integer(xx)[1] TRUE> xx <- seq.int(1,10, 2) > class(xx)[1] "numeric"> is.integer(xx)[1] FALSE> xx <- seq.int(1,10, 2L) > class(xx)[1] "numeric"> is.integer(xx)[1] FALSE> xx <- seq.int(1L, 10L, 2L) > class(xx)[1] "integer"> is.integer(xx)[1] TRUE I think all of those should have same return value, if the function is correctly named seq.int. -- Paul E. Johnson http://pj.freefaculty.org Director, Center for Research Methods and Data Analysis http://crmda.ku.edu I only use this account for email list memberships. To write directly, address me at pauljohn at ku.edu.
Marc Schwartz
2016-Aug-03 20:26 UTC
[Rd] seq.int does not return a sequence of integers sometimes
> On Aug 3, 2016, at 3:12 PM, Paul Johnson <pauljohn32 at gmail.com> wrote: > > I have a script that goes wrong because I assumed that seq.int would > return integers. > > Below please see it does not unless user is super cautious about > inserting "L" with inputs. I think seq.int should do coercion for me > before returning the sequence. > >> xx <- seq.int(1,10) >> class(xx) > [1] "integer" >> is.integer(xx) > [1] TRUE >> xx <- seq.int(1,10, 2) >> class(xx) > [1] "numeric" >> is.integer(xx) > [1] FALSE >> xx <- seq.int(1,10, 2L) >> class(xx) > [1] "numeric" >> is.integer(xx) > [1] FALSE >> xx <- seq.int(1L, 10L, 2L) >> class(xx) > [1] "integer" >> is.integer(xx) > [1] TRUE > > > I think all of those should have same return value, if the function is > correctly named seq.int.Paul, ?seq.int has the following: under Details: "seq.int is an internal generic which dispatches on methods for "seq" based on the class of the first supplied argument (before argument matching)." and under Value: "seq.int and the default method of seq for numeric arguments return a vector of type "integer" or "double": programmers should not rely on which." So:> is.integer(1)[1] FALSE> is.integer(1L)[1] TRUE which would seem to explain the behavior that you are observing. Regards, Marc Schwartz
Duncan Murdoch
2016-Aug-03 21:19 UTC
[Rd] seq.int does not return a sequence of integers sometimes
On 03/08/2016 4:12 PM, Paul Johnson wrote:> I have a script that goes wrong because I assumed that seq.int would > return integers."int" means "internal", not "integer". Sometimes you should read the documentation. Duncan Murdoch> > Below please see it does not unless user is super cautious about > inserting "L" with inputs. I think seq.int should do coercion for me > before returning the sequence. > >> xx <- seq.int(1,10) >> class(xx) > [1] "integer" >> is.integer(xx) > [1] TRUE >> xx <- seq.int(1,10, 2) >> class(xx) > [1] "numeric" >> is.integer(xx) > [1] FALSE >> xx <- seq.int(1,10, 2L) >> class(xx) > [1] "numeric" >> is.integer(xx) > [1] FALSE >> xx <- seq.int(1L, 10L, 2L) >> class(xx) > [1] "integer" >> is.integer(xx) > [1] TRUE > > > I think all of those should have same return value, if the function is > correctly named seq.int. >