Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

One of our readers commented on our mode exercises post: “What real world tasks are you using mode to solve?” I think it’s an interesting question, from a somewhat larger perspective. Obviously, it’d be a waste of time to learn all kinds of obscure commands that don’t have a clear application in the real world. Fortunately, that’s not the case with the mode function, and its cousins, as we’ll see shortly.

If you’re new to R, you might be particularly interested in getting up to speed quickly, and focus on those commands that produce a graph, table, or prediction, or jump straight to some of the popular packages that offer the latest data science toys.

If this approach works for you, great! Many R beginners will suffer unnecessarily however, by taking this route. Why? Read on…

## R and Italian, an analogy

Consider this analogy between learning R and learning a real language (as in, Italian or Chinese). Real languages have words that convey strong, clear messages (“Water”, “Money”, “Sleep”), as well al words that play a more supportive role (“you need”, “A little”, “Enough”), and words that may convey even less information (“Actually”, “Apparently”). (For more analogies like these, read this post on the ideas behind our exercise sets).

Likewise, R has commands that carry out big, important tasks with a lot of immediate practical value (plot, glm, table), as well as commands that play a more supportive role (data.frame, cut, as.Date).

## Master the full R vocabulary

My point is this: You need both the strong words/big commands, and the more supportive words/commands to get something done in practice, whether it’s in Italian (like, communicating: “Apparently, you need a little sleep”) or in R (like: reading some data, cleaning it, running a model and plotting the results).

Many basic R functions, like mode, play this supportive role. In and of itself, they don’t have much practical value, but they are important little nuts and bolts that are indispensable to get your ultimate task done. And are therefore important to master, as part of your R vocabulary!

## Practical uses of mode, some examples

Ok, so let’s get back to the original question and look at some practical uses of mode. Looking through some of my recent code, I noticed, I mostly use mode to convert characters to numbers and vice versa, and to check input values in functions.

## Ex. 1: Data cleaning

For example, your raw data might have typos or wrong characters, as in the following example, where the last element of the input vector contains the letter ‘o’, instead of the zero digit. Such data can only be read as character, so after reading it into R, you’d have something like the vector x below. Using this vector in a calculation would throw an error.

x <- c('20', '30', '4o')   # raw data,
x <- gsub('o', '0', x)     # cleaning
mode(x) <- 'numeric'       # convert from character to numeric
x + 1                      # we can now use x in calculations

## [1] 21 31 41


## Ex. 2: Checking input values

Another example of practical use of mode is to check input values in functions. Consider the sum function, which requires arguments of type numeric or logical. It will stop your script if it receives a character, e.g. sum(10, 'a'). Suppose this is not what you want. Instead you want a sum function that would simply return NA when it receives a character, without stopping the script. In that case you can use is.numeric() to check input values:

mysum <- function(a, b) {
if(is.numeric(a) & is.numeric(b)) a + b
else NA
}
mysum(10, 20)

## [1] 30

mysum(10, 'a')

## [1] NA


## Ex. 3: Subsetting

Finally, an example that involves subsetting of a table. Here we convert a numeric range (2005:2010) to character, to select a few columns (representing years) of a table:

df <- data.frame(years=1991:2010, v=sample(1:10, 20000, T))
mytable <- with(df, table(v, years))    # a cross-table with counts
mytable[, as.character(2005:2010)]

##     years
## v    2005 2006 2007 2008 2009 2010
##   1    92  108  115  110   90  119
##   2   101   82   91   98  103   90
##   3    96   99   93   89   84  100
##   4    99  107   92  103  108   97
##   5    99   99  118  118  104   81
##   6    94   89   93   89   95  108
##   7   104  125  108  111  115   95
##   8   116   85  100   97  108  101
##   9   109   95   94   91   89  102
##   10   90  111   96   94  104  107


Do you have other examples of practical uses of mode? Feel free to share below as a comment!

Image By Martorell – Self-published work by Martorell.