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

Petr Pikal petr.pikal at precheza.cz
Tue May 2 10:30:43 CEST 2006


I did not see any response so I try to express my opinion that the 
behaviour is quite ok.

In your first example there is no "NA" level so clearly the only NA 
stands for "not available". However in your second example there is 
"NA" as a level which stands for e.g. North America or New Account. 

fac<-as.factor("NA", "CD", NA)

retains NotAvailable on third place and 

gives you 2 different NA values, one with meaning and one which 
represents NotAvailable.

if you want NA to stays NA in your arguments you can try

> is.na(as.character(factor(c("NA", "CD", NA), exclude=NULL)))
> is.na(as.character(factor(c("AB", "CD", NA), exclude=NULL)))


On 28 Apr 2006 at 18:15, Brahm, David wrote:

Date sent:      	Fri, 28 Apr 2006 18:15:36 -0400
From:           	"Brahm, David" <David.Brahm at geodecapital.com>
To:             	"R-help" <r-help at stat.math.ethz.ch>
Subject:        	[R] as.character.factor when the factor contains "NA"

> 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)
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz

More information about the R-help mailing list