On Sat, Mar 9, 2013 at 6:50 PM, David Winsemius <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

______________________________________________
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.

Reply via email to