R tips and tricks – the assign() function

The R language has some quirks compared to other languages. One thing which you need to constantly watch for when moving to- or from R, is that R starts its indexing at one, while almost all other languages start indexing at zero, which takes some getting used to. Another quirk is the explicit need for clarity when modifying a variable, compared with other languages.

Take python for example, but I think it looks the same in most common languages:

Notice the very elegant and super clear
count += 1,
which says that count is going to be increased by 1. Now look at the R way:

You need to be clear about “running over” the variable count with the much lengthier
count <- count + 1.
I did not care before, but after tasting other languages I am now annoyed by this, like the ticking of a clock which went unnoticed until someone kindly directed your attention to that annoying ticking sound.

Can we do something about it? In short, not really. But perhaps an ugly workaround.
You can create a function to run over the it's own argument. Note that this goes against the R philosophy. Generally speaking the advice is: "Don't". But we, also, like to live dangerously..

We can use the assign() function to modify a function's argument like so:

This is ugly, but understandable, and closer to what you may be used to, coming from other languages.

A pipe-based alternative

I recently discovered the interesting and useful operator %<>% from the magrittr package in R. That operator applied to 'x' means: update 'x' and run over it. So the following also does the trick:

This looks slightly better. However, every time I use a piping operator I recall what I wrote in the past. Particularly regarding speed of execution when using pipes. So let's have a look at that:

You can see it was worth checking. Using the operator %<>% instead of our own "+1" function is very costly. Eight times slower.

Finally. let's look at the speed compared to what you would do by default in R, which is to explicitly specify an increase in the count:

Using the "+1" function allows for comparable readability and slightly better speed (~5% faster), without any additional package installations. May you find the assign function useful also in other contexts.

Leave a Reply

Your email address will not be published. Required fields are marked *