Fancy Cut

November 29, 2015
By

(This article was first published on TeachR, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

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)