Re: [R] adjusting "levels" after subset a table

From: Marc Schwartz <marc_schwartz_at_comcast.net>
Date: Sat, 08 Dec 2007 21:48:49 -0600

On Sat, 2007-12-08 at 19:26 -0800, Milton Cezar Ribeiro wrote:
> Dear all,
>
> I have a data.frame with a factor collumn with about 10 levels.
> After extract a subset of this data.frame, by selecting 2 of my 10
> levels, the new data.frame continue with original number of levels.
> How can I adjust it in a manner that when I try levels(my.df) I
> receive the actualyzed number of levels?
>
> By the way, I read my file using reab.table.
>
> I tryed solve it with : levels(my.df$my.var)<-unique(my.df$my.var)
> but the problem remain.
>
> Many thanks,
>
> miltinho
> Brazil

The default when subsetting factors (which happens when you subset a data frame) is to retain the original set of levels, even if they don't occur in the resultant subset. This is described in ?"[.factor" where the 'drop' argument is FALSE by default.

To subset the factor and only retain levels for those values that are still present, you can use:

  MyFactor <- factor(MyFactor)

or

  MyFactor <- MyFactor[, drop = TRUE]

after subsetting the data frame.

There is also a page in the R Wiki that describes some additional approaches:

http://wiki.r-project.org/rwiki/doku.php?id=tips:data-manip:drop_unused_levels

HTH, Marc Schwartz



R-help_at_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. Received on Sun 09 Dec 2007 - 03:56:23 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 Sun 09 Dec 2007 - 06:30:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.