R tips and tricks – utilities

As the title reads, few more R-related tips and tricks. I hope you have not seen those before.

Some utilities

Methods are functions which are specifically written for particular class. In the post Show yourself (look “under the hood” of a function in R) we saw how to get the methods that go with a particular class. Now there are more modern, less clunky ways for this.

See which methods are available per class

Have a look at the sloop package, maintained by Hadley Wickham (that alone is a reason). Use the function s3_methods_generic to get a nice table with some relevant information:

You can use the above to check if there exists a method for the class you are working with. If there is, you can help R by specifying that method directly. Do that and you gain, sometimes meaningfully so, a speed advantage. Let’s see how it works in couple of toy cases. One with a Date class and one with a numeric class.

Specifying the exact method (if it is there) also reduces the variance around computational time, which is important for simulation exercises:
Computation comparison of mean

Percent formatting

In the code snippet above I used the scales package’s percent function, which spares the formatting annoyance.

Get your object’s size

When I load a data, I often want to know how big is it. There is the basic object.size function but it’s ummm, ugly. Use the aptly named object_size function from the pryr package.

Memory management

Use the gc function; gc stands for garbage collection. It frees up memory by, well, collecting garbage objects from your workspace and trashing them. I at least, need to do this often.

heta function

I use the head and tail functions a lot, often as the first thing I do. Just eyeballing few lines helps to get a feel for the data. Default printing parameter for those function is 6 (lines) which is too much in my opinion. Also, especially with time series data you have a bunch of missing values at the start, or at the end of the time frame. So that I don’t need to run each time two function separately, I combined them into one:

Sound the alarm

If you stretch your model enough, you will have to wait until computation is done with. It is nice to get a sound notification for when you can continue working. A terrific way to do that is using the beepr package.

# install.packages("beepr")
for (i in 0:forever){
do many tiny calculations and don't ever converge

Click play to play:


A Survivor’s Guide to R

Leave a Reply

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