Re: [R] Factor function

From: Petr Savicky <>
Date: Mon, 25 Apr 2011 23:21:31 +0200

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. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 25 Apr 2011 - 21:29:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 25 Apr 2011 - 22:20:33 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive