Um texto embutido e sem conjunto de caracteres especificado associado... Nome: n?o dispon?vel Url: https://stat.ethz.ch/pipermail/r-help/attachments/20071208/5409f1a7/attachment.pl
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 > BrazilThe 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
Paulo Justiniano Ribeiro Jr
2007-Dec-09 03:52 UTC
[R] adjusting "levels" after subset a table
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:> 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 > > > > para armazenamento! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >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/