R Lesson 30: Logarithms

Advertisements

Hello everybody,

Michael here, and I hope you had a little fun with my two-part fifth anniversary post (particularly the puzzle). For the next several posts, we’ll be exploring more mathematics with R. Today we’ll discuss the wonderful world of R logarithms (and logarithms in general).

But first, a word on logarithms

As you may have noticed from my previous R mathematics posts, I always explain the concept in both the context of R and the context of manual calculation so you can have a basic understanding of the concept itself before exploring it in R. I plan to do the same thing here today.

So, what are logarithms exactly? They’re simply another type of mathematical operation. Take a look at this illustration below:

In this example, I used the simple example of 4^3=64. I also noted that the logarithmic form of this expression is log4(64)=3.

What does this mean? The base of the exponentional expression (4^3=64)-4-serves at the base of the logarithm. The number in parentheses-64-serves as the logarithm’s argument, which is the value used to find the logarithm’s exponent (or result), which is 3 in this case.

How do you read a logarithmic expression? In this example, you’d read the expression as log base-4 of 64 equals 3.

That’s how logarithms work. Now let’s explore how to manage them in R.

Logarithms, R style

So, how would you work with logarithms in R? Let’s take a look at the code below:

> log(32, base=2)
[1] 5

Calculating logs in R is as simple as using R’s built-in log() function and adding in two parameters-the argument and the base. With these two parameters and the log() function, R will return the logarithm’s exponent, which in this case is 5 since log base-2 of 32 equals 5.

However, what I discussed above was a very, very basic example of logarithms in R. Let’s look a more specific scenario:

> log10(1000)
[1] 3

In this example, I’m showing what is known as a base-10 logarithm, which is a logarithm with a base of, well, 10. In this case, I’m showing you what log base-10 of 1000 equals-in this case, the exponent/result is 3. Notice how this expression uses the log10() function rather than the log() function.

  • NOTE: To calculate this base-10 logarithm you can also use the code log(1000, base=10), but I just wanted to point out the log10() function as another way to solve a logarithm.

Another specific logarithmic scenario is binary, or base-2 logarithms. Let’s take a look at those:

> log2(8)
[1] 3

In this example, I’m showing a base-2 logarithm, which is a logarithm with a base of, well, 2. In this case, the base-2 log of 8 is 3, since 2^3=8.

  • NOTE: Just like the case with the base-10 logarithm, you can also use the code log(8, base=2) to calculate this base-2 algorithm.

Now let’s explore two rather unusual logarithmic scenarios-logarithms of imaginary numbers and logarithms with base e (e as in the mathematical constant). What does it all mean? Let me explain!

> log(11)
[1] 2.397895

> log(3+2i, base=4)
[1] 0.9251099+0.4241542i

The first example above shows the expression log base-e of 11 along with the result, 2.397895. The second example shows the expression log base-4 of 3+2i along with the result, 0.9251099+0.4241542i.

If you’re not familiar with the concept of imaginary numbers and the mathematical constant e, fear not, for I will explain both concepts right here!

A rational section about some irrational numbers

If you have even the most basic knowledge of numbers, you’ll be quite familiar with the numbers 0-9. After all, when you first learned basic counting, you very likely learned how to count to 10.

However, if you study more advanced math (like alegbra and calculus), you’ll notice there are a few numbers that aren’t so neat (and no, I’m not talking about decimals, fractions or negative numbers). These are known as irrational numbers, which are non-terminating, non-repeating numbers that can’t be expressed as simple fractions or decimals.

We just explored calculating logarithms with two types of irrational numbers-e and imaginary numbers. How do these types of numbers work?

In the case of e, e is a mathematical constant known as Euler’s number-named after Swiss mathematician Leonhard Euler. e is an irrational number that stretches on for infinity, but its approximate value is 2.71828. The number e is used in various exponential growth/decay functions, such as a city’s population growth/decline over a certain time period or a substance’s radioactive decay.

A log with a base e is also known as a natural logarithm (like the log(11) example I used earlier). In R, natural logarithms are denoted as log(number) with no base parameter specified. Here’s what natural logarithms look like:

In the case of the imaginary numbers, such as 3+2i, they provide an easy way to handle complex mathematical situations, such as the square roots of negative numbers (which will always be imaginary numbers). Imaginary numbers always consist of a real part multiplied by the imaginary unit i (such as 2i). The number 3+2i is known as a complex imaginary number since it has a real part (3) and an imaginary part (2i). One well known application of imaginary numbers is fractal geometry, which you can find quite a bit of in nature (like in conch shells).

Three more logarithmic scenarios that I think you should know

Before I go, I want to discuss three more logarithmic scenarios with you all, first with natural logs and then with logs that have a numerical base. Take a look at the code below:

> log(1)
[1] 0
> log(0)
[1] -Inf
> log(-12)
[1] NaN
Warning message:
In log(-12) : NaNs produced

In this example, I’m showing you three different natural logarithm scenarios that you should know-logs with arguments of 1, 0, and a negative number. Notice that a log with an argument of 1 yields 0, an log with an argument of 0 yields negative infinity and a log with a negative number yields an NaN (indicating that logs with negative arguments aren’t valid). Why might this be the case?

  • In the case of log(1), you get 0 because raising a number to the power of 0 always yields 1.
  • In the case of log(0), you get -Inf (negative infinity) because there is no possible power you can raise e to obtain 0.
  • In the case of log(-12), you get NaN (not a number) because logs only work with positive number arguments.

Now here are three scenarios with the same arguments, but using a base of 2 instead of e:

> log(1, base=2)
[1] 0
> log(0, base=2)
[1] -Inf
> log(-12, base=2)
[1] NaN
Warning message:
NaNs produced 

Notice how you get the same results here as you did for the natural logarithms.

Thanks for reading,

Michael