[R] Unexpected behavior looping through sequence of dates
Alexandre Sieira
alexandre.sieira at gmail.com
Sat Mar 9 23:19:26 CET 2013
Thanks for the clarification, Luke.
That is really counter-intuitive behavior. I 100% agree with you that the "for" documentation should state that assumption explicitly.
I would also like to suggest changing the "for" implementation to issue a warning if the "seq" argument is a vector of a non-primitive type. That would have saved me a few hours of debugging in the last few days, so I imagine it must be relevant for more people as well.
--
Alexandre Sieira
CISA, CISSP, ISO 27001 Lead Auditor
"The truth is rarely pure and never simple."
Oscar Wilde, The Importance of Being Earnest, 1895, Act I
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)
On Saturday, 9 de March de 2013 at 13:10, luke-tierney at uiowa.edu wrote:
> R's for loop is only designed to iterato over primitive types. The
> help file says of the seq argument:
>
> seq: An expression evaluating to a vector (including a list and an
> expression) or to a pairlist or ¡NULL¢. A factor value will
> be coerced to a character vector.
>
> [This could be more emphatic by stating that any class attributes are
> igonred or something of that nature.]
>
> Having for() do anything else would require designing an iteration
> protocol -- probably would be nice in principle but not easy to do.
>
> Best,
>
> luke
>
> On Sat, 9 Mar 2013, Peter Ehlers wrote:
>
> > On 2013-03-09 11:14, R. Michael Weylandt wrote:
> > > On Sat, Mar 9, 2013 at 6:50 PM, David Winsemius <dwinsemius at comcast.net (mailto:dwinsemius at comcast.net)>
> > > wrote:
> > > > I was unable to find the reason for the original coercion in the
> > > > help("for") page or the R
> > > > Language Definition entry regarding for-loops. On the hunch that coercion
> > > > via as.vector
> > > > might be occurring,
> > >
> > >
> > >
> > > Behaviorally, it seems to, but the code for do_for in eval.c has
> > > factors special-cased to call
> > > asCharacterFactor so that might not be a robust detail to rely on. The
> > > relevant behavior seems instead to be that there's a
> > > switch on val_type which creates the loop index but doesn't copy all
> > > attributes (like class)
> > >
> > > Note that this means a user's as.vector wouldn't be called here:
> > >
> > > as.vector.flub <- function(x, ...) letters[x]
> > >
> > > foo <- 1:5
> > > class(foo) <- "flub"
> > >
> > > as.vector(foo)
> > >
> > > for(j in foo) {print(j); print(class(j))}
> > >
> > > as.vector.grub <- function(x, ...) match(x, letters)
> > >
> > > bar <- letters[1:5]
> > > class(bar) <- "grub"
> > >
> > > as.vector(bar)
> > >
> > > for(j in bar) {print(j); print(class(j))}
> > >
> > > Cheers,
> > > Michael
> >
> >
> >
> > I think that Michael is right - the problem is with val_type
> > in the do_for code.
> >
> > Here's a simplified version of Alexandre's example:
> >
> > d <- as.Date("2013-03-10")
> > for(i in seq_along(d)) print(i)
> > #[1] 1
> >
> > for(i in d) print(i)
> > #[1] 15774
> > where we might have expected to see "2013-03-10".
> >
> > The essential line in the do_for code seems to me to be:
> >
> > val_type = TYPEOF(val);
> >
> > ?typeof tells us that R does not have a 'date' type, so:
> >
> > typeof(d)
> > #[1] "double"
> >
> > And the for-loop results follow.
> >
> > Peter Ehlers
> >
> > ______________________________________________
> > R-help at r-project.org (mailto:R-help at r-project.org) mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Luke Tierney
> Chair, Statistics and Actuarial Science
> Ralph E. Wareham Professor of Mathematical Sciences
> University of Iowa Phone: 319-335-3386
> Department of Statistics and Fax: 319-335-3017
> Actuarial Science
> 241 Schaeffer Hall email: luke-tierney at uiowa.edu (mailto:luke-tierney at uiowa.edu)
> Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
More information about the R-help
mailing list