Skip to content

adjusting "levels" after subset a table

3 messages · Milton Cezar Ribeiro, Marc Schwartz, Paulo Justiniano Ribeiro Jr

#
On Sat, 2007-12-08 at 19:26 -0800, Milton Cezar Ribeiro wrote:
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
#
running
my.df$my.var <- factor(my.df$my.var)

seems to do the job

P.J.
On Sat, 8 Dec 2007, Milton Cezar Ribeiro wrote:

            
Paulo Justiniano Ribeiro Jr
LEG (Laboratorio de Estatistica e Geoinformacao)
Universidade Federal do Parana
Caixa Postal 19.081
CEP 81.531-990
Curitiba, PR  -  Brasil
Tel: (+55) 41 3361 3573
Fax: (+55) 41 3361 3141
e-mail: paulojus AT  ufpr  br
http://www.leg.ufpr.br/~paulojus

-------------------------------------------------------------------------
53a Reuniao Anual da Regiao Brasileira da Soc. Internacional de Biometria
14 a 16/05/2008, UFLA, Lavras,MG
http://www.centenario.ufla.br/