Re: [R] Unexpected behavior looping through sequence of dates
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-tier...@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 > > (mailto:dwinsem...@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@r-project.org (mailto:R-help@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-tier...@uiowa.edu > (mailto:luke-tier...@uiowa.edu) > Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu __ R-help@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.
Re: [R] Unexpected behavior looping through sequence of dates
On Sat, 9 Mar 2013, Alexandre Sieira wrote: 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. Issuing a warning unconditioally would result in far to many spurious warnings. for() works perfecly fine now for most objects with a class attribute. I don't see a reasonable way to detect the subset of cases where this is not true (other than registering a list of classes for() should warn about, which might be feasible, though might refult in a significant performance hit.) Best, luke -- 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-tier...@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 mailto:dwinsem...@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@r-project.org (mailto:R-help@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-tier...@uiowa.edu (mailto:luke-tier...@uiowa.edu) Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu -- Luke Tierney Chair, Statistics and Actuarial Science Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics andFax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke-tier...@uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu__ R-help@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.
Re: [R] Unexpected behavior looping through sequence of dates
On Mar 9, 2013, at 1:10 PM, 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. except it works fine with lists: for ( i in as.list(seq(Sys.Date(), length=4,by=1) ) ) print(class(i)) [1] "Date" [1] "Date" [1] "Date" [1] "Date" > > 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 >>> 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@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 andFax: 319-335-3017 > Actuarial Science > 241 Schaeffer Hall email: luke-tier...@uiowa.edu > Iowa City, IA 52242 WWW: > http://www.stat.uiowa.edu__ > R-help@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. David Winsemius Alameda, CA, USA __ R-help@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.
Re: [R] Unexpected behavior looping through sequence of dates
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 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@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 andFax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke-tier...@uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu__ R-help@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.
Re: [R] Unexpected behavior looping through sequence of dates
On 2013-03-09 11:14, R. Michael Weylandt wrote: On Sat, Mar 9, 2013 at 6:50 PM, David Winsemius 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@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.
Re: [R] Unexpected behavior looping through sequence of dates
On Sat, Mar 9, 2013 at 6:50 PM, David Winsemius 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 __ R-help@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.
Re: [R] Unexpected behavior looping through sequence of dates
On Mar 9, 2013, at 9:24 AM, Alexandre Sieira wrote: > I understand that the two following loops should produce the exact same > output. However, they do not. It appears that looping directly through the > sequence of Date objects somehow makes them be coerced to numeric: > >> date1 = "20130301" >> date2 = "20130302" >> >> d1 = as.Date(date1, format="%Y%m%d", tz="GMT") >> d2 = as.Date(date2, format="%Y%m%d", tz="GMT") >> >> range = seq(from=d1, to=d2, by="day") >> for (i in 1:length(range)) print(class(range[i])) > > [1] "Date" > [1] "Date" >> >> for (i in seq(from=d1, to=d2, by="day")) print(class(i)) > [1] "numeric" > [1] "numeric" > > This is output from R version 2.15.2 (2012-10-26) on Mac OS X 10.8.2. > > Is this expected behavior? It wasn't expected by me either, but it is what both my Mac R 2.15.3 and my Windows(32bit) 2.15.1 display. If you put those dates in a list, you do not get the coercion: > for (i in list(d1,d2)) print(class(i)) [1] "Date" [1] "Date" 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, I tried with with a factor vector and did see that it was coerced to "character" which is the behavior that would be expected if that were happening behind the scenes: ff <- factor(letters[1:3]) as.vector(ff) #[1] "a" "b" "c" for (i in ff) print(class(i)) #[1] "character" #[1] "character" #[1] "character" (I also tried searching for loop in the R Internals document and found nothing that explained this to me.) -- David. > -- > 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) > > __ > R-help@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. David Winsemius Alameda, CA, USA __ R-help@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.