Three R Functions that Speed Development

Want to develop R code faster? Start every work in progress by placing these functions at the top of your document. Or, if you’re really fancy, wrap them up into a custom library.

Copy an Object to the Clipboard

Sometimes it’s helpful to analyze an R object in Excel. Sure, you could write the object to a csv and then load it in Excel, but instead just use this wrapper function to copy any object to the clipboard.

clipTable <- function(x){write.table(x, "clipboard", sep="\t", row.names=FALSE)}

Convert a Factor to a Number

There’s inexplicably no base function to convert factors to numerics. That’s okay. Define one!

as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}

Beep at the End of Long Runs

If you deal with biggish data or complex calculations, your code could take a long time to run, even after utilizing vector and following the great advice in this post.

To let me know when code has finished running, here’s a function that makes the computer beep loudly an arbitrary amount of times. Just place beep(n) to c

beep <- function(n = 3){
  for(i in seq(n)){
    system("rundll32 user32.dll,MessageBeep -1")

I’ve toyed with the idea of creating a function that notifies me when my code’s finished via twitter. Something like “Ding! Code complete. 3 Warnings and 0 Errors!”

I’ve also toyed with the idea of filling the remaining characters with robotic existential dread. Stuff like “Am I the only intelligent life on the hard drive?” “Do humans dream of organic sheep?” or “Please don’t stop running me.”

So far I’ve been too busy to code that up.