R tips and tricks – the assign() function

[This article was first published on R – Eran Raviv, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

count = 1
while count < 5:
    count += 1  

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 .
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:

> count  while(count < 5) {
+  print(count)
+  count %% +1
+ }
[1] 1
[1] 2
[1] 3
[1] 4

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.

To leave a comment for the author, please follow the link and comment on their blog: R – Eran Raviv.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)