[R] Factor function

Petr Savicky savicky at praha1.ff.cuni.cz
Mon Apr 25 23:21:31 CEST 2011


On Mon, Apr 25, 2011 at 12:53:40PM -0700, Lisa wrote:
> Dear All,
> 
> I just want to remove “NA” from the levels of a factor.  For example:
> 
> d<-data.frame(matrix(c("ww","ww","xx","yy","ww","yy","xx","yy","NA"),
> ncol=3, byrow=TRUE))
> 
> > factor(d[, 3], exclude=NA)
> [1] xx yy NA
> Levels: NA xx yy
> 
> But “NA” is still listed in the levels. How can I solve this problem?

The column d[, 3] is already a factor. It is possible to avoid
this using

  d<-data.frame(matrix(c("ww","ww","xx","yy","ww","yy","xx","yy","NA"),
  ncol=3, byrow=TRUE), stringsAsFactors=FALSE)

Then, we get

  factor(d[, 3], exclude="NA")

  [1] xx   yy   <NA>
  Levels: xx yy

Hope this helps.

Petr Savicky.



More information about the R-help mailing list