Search code examples
rhmisc

How To Filter A Dataframe On a Factor


I used cut2 from the Hmisc package to create grouped PenRages in my data1 dataframe below. How do I filter my dataframe on the PenRanges so that I only get the rows with a group of [ 0.0, 12.8)?

         District     Retail      Loans    Penetration    PenRanges
         24           746982    53471.38    7.158322      [ 0.0, 12.8)
         23           182797   101471.48   55.510473      [53.0,114.3]
         13          204865    97036.50   47.366070       [39.8, 53.0)
         13          935916   315321.53   33.691221       [26.1, 39.8)

I tried pen1 <- subset(data1, PenRanges=="0.0, 12.8"), but it did not work.


Solution

  • The "[" and ")" are a proper part of the factor level. You need to match the value exactly as you see it

    pen1 <- subset(data1, PenRanges=="[ 0.0, 12.8)")
    

    should work. Or assuming the levels are ordered and you want the min:

    pen1 <- subset(data1, PenRanges==levels(PenRanges)[1])
    

    prevents you from having to retype the messy cut name.