The world’s leading publication for data science, AI, and ML professionals.

Advanced User Defined functions in R

Have you come across situations where you wish there was a built in function to achieve a result rather than writing multiple lines of…

Have you come across situations where you wish there was a built in function to achieve a result rather than writing multiple lines of code for it? Do you want to know how to optimize your R code? Did you know that you can write R codes easily using UDFs? UDFs are basic building blocks that you can plug and play while writing your code. Read on to know more…

Photo by Ryan Quintal on Unsplash
Photo by Ryan Quintal on Unsplash

UDFs (User Defined Functions) makes your code structured, helps easily identify errors & makes it much easier for alterations in future. Once you are used to writing and defining UDFs, there is no turning back! You will be obsessed with how useful they are to you.

If you are a beginner, and want to know the basics of how user defined functions are created, please go through my article mentioned below:

Your first user defined function in R

Now that we are all prepped up, lets get started with some examples of advanced UDFs.

Using multiple ‘if conditions’ inside a UDF

UDFs are flexible and can be defined by having multiple ‘if conditions’ and built in functions within a UDF. Please find a function in the link below for temperature conversions from Celsius to Fahrenheit and vice versa. I have used simple ‘if conditions’ and built in functions to create this UDF.

shruthigeetha/R_UDFunctions

Now this UDF can be modified to include other temperature units and many more fail safe methods. Do feel free to contribute.

Running ‘for loops’ inside a UDF

For loops can be used inside UDFs to make sure that a similar operation is performed on a subset of data rather than on the complete data together.

The results in example shown below can be obtained in multiple ways like using simple aggregate functions or apply functions. With this example, I would like to put forward one of the non-traditional methods to get monthly information from daily data using a UDF. This UDF can be used on any similar datasets to obtain monthly aggregated information.

shruthigeetha/R_UDFunctions

UDFs within a UDF

Here is when things get much more interesting!

Ever used a UDF within a UDF? Now that you are all pros, we can have nested UDFs that can help in simplifying you code even more. This one big leap towards writing compact codes that are easy to interpret and decode!

For this, I will be using the previous examples of Temperature conversion and Monthly data extraction to show you an example of UDF within a UDF.

shruthigeetha/R_UDFunctions

In the above example, I have obtained monthly information with Temperature in Celsius scale rather than in the original Fahrenheit scale.

What’s next?

Now is when you can experiment and start using UDFs in your daily R codes. From the examples above, we can clearly see how flexible and easy it is to create reusable and multi-purpose UDFs.

If you can you think of other scenarios where UDFs can be applied or if you have any themes in mind that can make use of UDFs, please feel free to comment or open a discussion forum.


Related Articles