Re: [R] Remove empty levels in subset
Hi, I have exactly the same question (how to remove empty levels in my subset), but in my case the factor command does not work, because my dataframe is not atomic Try this: test2$a - factor(test2$a) R gives me the error message: Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list? Do you have advice? Thank you -- View this message in context: http://r.789695.n4.nabble.com/Remove-empty-levels-in-subset-tp873967p4634499.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Remove empty levels in subset
Hi, On Tue, Jun 26, 2012 at 8:06 AM, svo s.vanom...@uu.nl wrote: Hi, I have exactly the same question (how to remove empty levels in my subset), but in my case the factor command does not work, because my dataframe is not atomic Try this: test2$a - factor(test2$a) R gives me the error message: Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list? Do you have advice? I have two pieces of advice. 1. Don't try to use factor() on your entire data frame, but only on a single column at a time, as shown in the example you included. 2. Provide an example of your data using something like dput(head(mydata, 10)) so we can offer actual working code. Sarah -- Sarah Goslee http://www.functionaldiversity.org __ R-help@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.
Re: [R] Remove empty levels in subset
Thank you very much. The advice I followed (and which, for some reason, I do not see here right now) was to use 'droplevels'. I needed the command for several variables at the same time, so this was very convenient. Hello, Have you tried 'droplevels': test -data.frame(a=as.factor(rep(c(f1,f2,f3),10)),b=rep(c(1,2,3),10)) test2 - subset(test,test$a==f1) summary(test2) ab f1:10 Min. :1 f2: 0 1st Qu.:1 f3: 0 Median :1 Mean :1 3rd Qu.:1 Max. :1 test3-droplevels(test2) summary(test3) ab f1:10 Min. :1 1st Qu.:1 Median :1 Mean :1 3rd Qu.:1 Max. :1 A.K. -- View this message in context: http://r.789695.n4.nabble.com/Remove-empty-levels-in-subset-tp873967p4634550.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Remove empty levels in subset
Thanks it work perfectly. 2008/11/10 Henrique Dallazuanna [EMAIL PROTECTED] Try this: test2$a - factor(test2$a) On Mon, Nov 10, 2008 at 10:33 AM, Ronaldo Reis-Jr. [EMAIL PROTECTED]wrote: Hi, when I use use subset in a data.frame, all empty levels are maintained in the new table. test - data.frame(a=as.factor(rep(c(f1,f2,f3),10)),b=rep(c(1,2,3),10)) summary(test) ab f1:10 Min. :1 f2:10 1st Qu.:1 f3:10 Median :2 Mean :2 3rd Qu.:3 Max. :3 test2 - subset(test,test$a==f1) summary(test2) ab f1:10 Min. :1 f2: 0 1st Qu.:1 f3: 0 Median :1 Mean :1 3rd Qu.:1 Max. :1 Look that the f2 and f3 are in the new table, how to remove all empty levels? Thanks Ronaldo [[alternative HTML version deleted]] __ R-help@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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Remove empty levels in subset
Try this: test2$a - factor(test2$a) On Mon, Nov 10, 2008 at 10:33 AM, Ronaldo Reis-Jr. [EMAIL PROTECTED]wrote: Hi, when I use use subset in a data.frame, all empty levels are maintained in the new table. test - data.frame(a=as.factor(rep(c(f1,f2,f3),10)),b=rep(c(1,2,3),10)) summary(test) ab f1:10 Min. :1 f2:10 1st Qu.:1 f3:10 Median :2 Mean :2 3rd Qu.:3 Max. :3 test2 - subset(test,test$a==f1) summary(test2) ab f1:10 Min. :1 f2: 0 1st Qu.:1 f3: 0 Median :1 Mean :1 3rd Qu.:1 Max. :1 Look that the f2 and f3 are in the new table, how to remove all empty levels? Thanks Ronaldo [[alternative HTML version deleted]] __ R-help@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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] __ R-help@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.