[R] Is this foreach behaviour correct?
Henrik Bengtsson
henrik.bengtsson at gmail.com
Sun Nov 13 14:04:27 CET 2016
On Nov 13, 2016 13:54, "Henrik Bengtsson" <henrik.bengtsson at gmail.com>
wrote:
>
> It looks like a bug. I don't think c.Date() is every called, because:
>
> > trace(c.Date, tracer = quote(message("c.Date() called")))
> Tracing function "c.Date" in package "base"
> [1] "c.Date"
>
> Tracing works:
>
> > c(as.Date(10000L), as.Date(10001L))
> Tracing c.Date(as.Date(10000L), as.Date(10001L)) on entry
> c.Date() called
> [1] "1997-05-19" "1997-05-20"
>
> but c.Date() is not called here:
>
> > x <- foreach(i=10000:10100, .combine = function(...) c(...)) %do% {
as.Date(i) }
> > str(x)
> num [1:101] 10000 10001 10002 10003 10004 ...
Cut'n'paste error above. It is
x <- foreach(i=10000:10100, .combine = "c") %do% { as.Date(i) }
that doesn't call c.Date(). Same if you try with .combine = c.
>
>
> The following hack works:
>
> > library("foreach")
> > library("zoo")
> > x <- foreach(i=10000:10100, .combine = function(...) c(...)) %do% {
as.Date(i) }
> > str(x)
> Date[1:101], format: "1997-05-19" "1997-05-20" "1997-05-21" "1997-05-22"
...
>
> Alternatively, one can use append() which works like c() if no other
> arguments are specified:
>
> > x <- foreach(i=10000:10100, .combine = append) %do% { as.Date(i) }
> > str(x)
> Date[1:101], format: "1997-05-19" "1997-05-20" "1997-05-21" "1997-05-22"
...
>
> It looks like foreach is treating the .combine = c case specially and
> someone fail to properly dispatch c() on the object (or something).
>
> /Henrik
>
>
> On Sun, Nov 13, 2016 at 7:14 AM, James Hirschorn
> <james.hirschorn at hotmail.com> wrote:
> > I'm still not clear about whether this is a bug in foreach. Should
c.Date be invoked by foreach with .combine='c'?
> >
> > On 11/06/2016 07:02 PM, William Dunlap wrote:
> > Note that in the OP's example c.Date is never invoked. c.Date is
called if .combine
> > calls c rather than if .combine is c:
> >
> >> library(zoo)
> >> trace(c.Date, quote(print(sys.call())))
> > Tracing function "c.Date" in package "base"
> > [1] "c.Date"
> >> foreach(i=10000:10003, .combine=c) %do% { as.Date(i) }
> > [1] 10000 10001 10002 10003
> >> foreach(i=10000:10003, .combine=function(...)c(...)) %do% { as.Date(i)
}
> > Tracing c.Date(...) on entry
> > eval(expr, envir, enclos)
> > Tracing c.Date(...) on entry
> > eval(expr, envir, enclos)
> > Tracing c.Date(...) on entry
> > eval(expr, envir, enclos)
> > [1] "1997-05-19" "1997-05-20" "1997-05-21" "1997-05-22"
> >
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com<http://tibco.com>
> >
> > On Sun, Nov 6, 2016 at 2:20 PM, Duncan Murdoch <murdoch.duncan at gmail.com
<mailto:murdoch.duncan at gmail.com>> wrote:
> > On 06/11/2016 5:02 PM, Jim Lemon wrote:
> > hi James,
> > I think you have to have a starting date ("origin") for as.Date to
> > convert numbers to dates.
> >
> > That's true with the function in the base package, but the zoo package
also has an as.Date() function, which defaults the origin to "1970-01-01".
If James is using zoo his code would be okay. If he's not, he would have
got an error, so I think he must have been.
> >
> > Duncan Murdoch
> >
> >
> >
> > Jim
> >
> > On Sun, Nov 6, 2016 at 12:10 PM, James Hirschorn
> > <james.hirschorn at hotmail.com<mailto:james.hirschorn at hotmail.com>> wrote:
> > This seemed odd so I wanted to check:
> >
> > > x <- foreach(i=10000:10100, .combine='c') %do% { as.Date(i) }
> >
> > yields a numeric vector for x:
> >
> > > class(x)
> > [1] "numeric"
> >
> > Should it not be a vector of Date?
> >
> > ______________________________________________
> > R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To
UNSUBSCRIBE and more, see
> > 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.
> >
> > ______________________________________________
> > R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To
UNSUBSCRIBE and more, see
> > 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.
> >
> >
> > ______________________________________________
> > R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To
UNSUBSCRIBE and more, see
> > 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.
> >
> >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
Henrik
[[alternative HTML version deleted]]
More information about the R-help
mailing list