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

How I Made My First R Function

And how you can too: a real-world example.

Learning R is a real pain in the keister – the learning curve is steep and it can be overwhelming. What keeps me coming back to this humbling journey is the capability to only have to do the work for something once and then repeat it as many times as I need. I love efficiency so I soldier on. Today I ran into a tedious annoyance that I was able to solve in minutes by creating a function.

The situation

I am looking for a new apartment and trying to compare prices. Fortunately (thanks, COVID), some units are offering a free month or two (!) of rent. This is fantastic news but it makes it difficult to compare prices.

What I was doing

When I saw a listing with a discounted rent, I would open up my calculator app and do:

  1. Number of months free (discount) * monthly rent (base rent)
  2. Minimum lease term (usually 12 months) * monthly rent (base rent)
  3. Subtract what I got from #1 from #2 and divide that by the minimum lease length requirement
Photo by Kelly Sikkema on Unsplash
Photo by Kelly Sikkema on Unsplash

If I was doing this once, or even twice, it wouldn’t be that bad. But I started getting frustrated when went to do this a third time. I knew the exact steps that I wanted to be performed each time but I had to do each one manually – yuck!

R functions to the rescue!

R function basics

I was really intimidated writing a function for the first time in R but it helped to break it down piece-by-piece. Keep in mind, functions can get complicated quickly but we will keep things as simple as possible.

At its most basic, a function in R has this structure:

Created in Carbon.
Created in Carbon.

Not too scary, right? A function is really just:

  1. The name of your function (myfunction)
  2. The inputs or data needed to perform your function (inputs)
  3. The steps you want to be completed

An example of a function is mean(). When you want the mean calculated, R knows to add all the numbers and then divide by how many numbers there are. Instead of having to add the numbers ourselves and then divide, we can just use the mean function. Because I also have fixed steps that I want to be repeated every time, functions are a great solution.

My R function

Let’s go back to my renting example and identify the inputs and the steps to be completed.

The inputs that I need to calculate what my effective rent would be are the base rent, the discount (the number of free months being offered), and the term (usually 12 months).

The steps I want to be completed are to: multiply the base amount the term length minus the base amount the discount. I then want to divide that by the term to see how much I will effectively be paying each month of the term after the discount is applied.

I am going to call this function myrent and i’m going to tell it that we need three inputs: the base rent (base), the number of months free (discount), and the lease term (term).

We haven’t told R anything yet about what those inputs (base, discount, and term) mean. We could also use x, y, z. All we are doing now is telling it that we are going to need these three pieces of information and then we are going to do something with them.

Now for the steps. We are going to kindly tell our fRiend (I crack myself up) the steps that we want to be completed and put them within {}.

Things are coming along! Just one last step, we need to ask R to show us what it calculated after completing the steps that we specified. So we are going to ask R to print "result" which is the answer to (baseterm-basediscount)/term.

Voila, we are ready to try out using our function!!! Let’s say that I see an apartment that is $2,000 per month (yes, it kills me too, DC housing is a real S.O.B.) and they are offering 1 month free for a 12-month lease.

Now that we have created the function, all we have to do is call it using "myrent" and enter in each of the inputs (base, discount, and term).

And just like that our trusty buddy gives us [1] 1833.333.

This means that if the regular rent is $2,000 per month and there is a discount being offered for two free months, I would effectively be paying $1,833.33 each month for 12 months. I can now easily compare that to an apartment that is $1,920 per month that is offering a discount for one free month. Magic!

Keep knocking your head against the wall, it gets so much better!


By: Jenna EaglesonMy background is in Industrial-Organizational Psychology and I have found my home in People Analytics. Data viz is what makes my work come to life. I mostly use Power BI but I enjoy dabbling in other tools. I would love to hear more about your journey! Reach me by commenting here or on Twitter or Linkedin.


Related Articles