Fancy Cut

[This article was first published on TeachR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

About a year ago I was working on an analysis that required me to take about 20 continuous variables and turn each into a factor based on some banding rule. The rules were complicated enough that using cut() was painful.

cut() works great when the banding you want to do is simple. For example,

vals <- c(22, 69, 89, 56, 33, 57, 10, 38, 50, 25)
cut(
  vals, 
  breaks = c(0, 50, 65, 100),
  labels = c('Low', 'Med', 'High')
)

##  [1] Low  High High Med  Low  Med  Low  Low  Low  Low 
## Levels: Low Med High

To leave a comment for the author, please follow the link and comment on their blog: TeachR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)