12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

following:> frost head(frost, 5)Alabama Alaska Arizona Arkansas California20 152 15 65 20You now have a new object, frost, a named numeric vector. Now use cut() tocreate three bins in your data:> cut(frost, 3, include.lowest=TRUE)[1] [-0.188,62.6] (125,188] [-0.188,62.6] (62.6,125][5] [-0.188,62.6] (125,188] (125,188] (62.6,125]....[45] (125,188] (62.6,125] [-0.188,62.6] (62.6,125][49] (125,188] (125,188]Levels: [-0.188,62.6] (62.6,125] (125,188]The result is a factor with three levels. The names of the levels seem a bitcomplicated, but they tell you in mathematical set notation what the boundaries ofyour bins are. For example, the first bin contains those states that have frostbetween –0.188 and 62.8 days. In reality, of course, none of the states will havefrost on negative days — R is being mathematically conservative and adds a bit ofpadding.Note the argument include.lowest=TRUE to cut(). The default value for thisargument is include.lowest=FALSE, which can sometimes cause R to ignorethe lowest value in your data.Adding labels to cutThe level names aren’t very user friendly, so specify some better names withthe labels argument:> cut(frost, 3, include.lowest=TRUE, labels=c(“Low”, “Med”, “High”))[1] Low High Low Med Low High High Med Low Low Low....[45] High Med Low Med High HighLevels: Low Med HighNow you have a factor that classifies states into low, medium, and high,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!