The handy cut()

One of the most interesting functions for a bioinformatician in R is cut()

Want to a generate asthetically pleasing window size label given an integer?

getWindowLabel <- function(x) {
    ind <- cut(abs(x), c(0, 1000, 1e+06, 1e+09, 1e+12), include.lowest = TRUE, 
                  right = FALSE, labels = FALSE)
    paste(x/c(1, 1000, 1e+06, 1e+09, 1e+12)[ind], 
          c("bp", "Kb", "Mb", "Gb")[ind], sep = "")

getWindowLabel(c(0, 1e+07, 1000, 1e+06, 2e+09))

[1] "0bp"  "10Mb" "1Kb"  "1Mb"  "2Gb" 

How about those significant p-values?

getStars <- function(x) 
     as.character(cut(x, c(0, 0.001, 0.01, 0.05, 1), 
                  c("***", "**", "*", ""), 
                  include.lowest = TRUE))

getStars(c(0, 0.001, 0.01, 0.05, 1))

[1] "***" "***" "**"  "*"   ""

You can use cut() to get equally sized bins of datapoints or convert a numeric variable into a categorical type.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.