Dan Abner
2011-Sep-01 18:59 UTC
[R] Including only a subset of the levels of a factor XXXX
Hello everyone, I have the following factor: levels(pp_income) [1] "" "1" "2" "3" "4" "5" "6" "7" [9] "8" "9" "Renter" I want to subset so that only values 1:9 are included. I have the following:> income<-pp_income[pp_income %in% c(1:9)] > > levels(income)[1] "" "1" "2" "3" "4" "5" "6" "7" [9] "8" "9" "Renter" Why is this not working and can someone please suggest a solution? Thank you! Dan [[alternative HTML version deleted]]
R. Michael Weylandt
2011-Sep-01 19:11 UTC
[R] Including only a subset of the levels of a factor XXXX
Dropping all occurences of a factor does not drop that level. This actually turns out to be much more useful than it first might appear, but if you really need to get around it, it can be done. Look at this toy example: R> x = factor(c("A","B","C","A","B","C","C")) R> x [1] A B C A B C C Levels: A B C R> x[x != "C"] [1] A B A B Levels: A B C R> y = factor(1:10) R> y [1] 1 2 3 4 5 6 7 8 9 10 Levels: 1 2 3 4 5 6 7 8 9 10 R> y[y != 5] [1] 1 2 3 4 6 7 8 9 10 Levels: 1 2 3 4 5 6 7 8 9 10 If you really need the level change, search the history of this list for mentions of "recode" (it comes up quite frequently) Michael On Thu, Sep 1, 2011 at 2:59 PM, Dan Abner <dan.abner99@gmail.com> wrote:> Hello everyone, > > I have the following factor: > > levels(pp_income) > [1] "" "1" "2" "3" "4" "5" "6" "7" > [9] "8" "9" "Renter" > > I want to subset so that only values 1:9 are included. I have the > following: > > > income<-pp_income[pp_income %in% c(1:9)] > > > > levels(income) > [1] "" "1" "2" "3" "4" "5" "6" "7" > [9] "8" "9" "Renter" > > Why is this not working and can someone please suggest a solution? > > Thank you! > > Dan > > [[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. >[[alternative HTML version deleted]]
David Winsemius
2011-Sep-01 19:29 UTC
[R] Including only a subset of the levels of a factor XXXX
On Sep 1, 2011, at 2:59 PM, Dan Abner wrote:> Hello everyone, > > I have the following factor: > > levels(pp_income) > [1] "" "1" "2" "3" "4" "5" "6" "7" > [9] "8" "9" "Renter" > > I want to subset so that only values 1:9 are included. I have the > following: > >> income<-pp_income[pp_income %in% c(1:9)] >> >> levels(income) > [1] "" "1" "2" "3" "4" "5" "6" "7" > [9] "8" "9" "Renter" > > Why is this not workingActually it could be that it did succeed but you just have levels attributes that are unpopulated in your result. Try: table{income) If that looks correct, then do this: income <- factor(income) # will drop unused levels> and can someone please suggest a solution?If on the other hand you got the wrong values then there was an undesired coercion of either 'factor' class to 'numeric' or of 'numeric' to 'character'. I am fairly sure this will remove any ambiguity: income<-pp_income[as.character(pp_income) %in% as.character(1:9)] (You would still get the puzzling extra levels if you looked with levels(income).)> > Thank you! > > Dan > > [[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.David Winsemius, MD West Hartford, CT