[R] Is this foreach behaviour correct?

James Hirschorn james.hirschorn at hotmail.com
Sun Nov 13 07:14:49 CET 2016


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]]



More information about the R-help mailing list