[R] as.character.factor when the factor contains "NA"

Brahm, David David.Brahm at geodecapital.com
Sat Apr 29 00:15:36 CEST 2006


as.character.factor contains this line (where cx=levels(x)[x]):
  if ("NA" %in% levels(x)) cx[is.na(x)] <- "<NA>"

Is it possible that this is no longer the desired behavior?  These
two results don't seem very consistent:

> as.character(as.factor(c("AB", "CD", NA)))
[1] "AB" "CD" NA  
> is.na(.Last.value)[3]
[1] TRUE

> as.character(as.factor(c("NA", "CD", NA)))
[1] "NA"   "CD"   "<NA>"
> is.na(.Last.value)[3]
[1] FALSE

I'm using R-2.3.0 on Redhat Linux, but I don't think the behavior
is new (maybe since character NA's were introduced?).

-- David Brahm (brahm at alum.mit.edu)




More information about the R-help mailing list