[R] Unexpected behavior looping through sequence of dates
R. Michael Weylandt
michael.weylandt at gmail.com
Sat Mar 9 20:14:37 CET 2013
On Sat, Mar 9, 2013 at 6:50 PM, David Winsemius <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
More information about the R-help
mailing list