# Logarithm in R – 4 variants to Know!

Filed Under: R Programming Hello, readers! In this article, we would be focusing on the Various functions of Logarithm in R programming. So let us begin!! 🙂

Logarithmic functions serve us an important aspect in terms of scaling of the data values and even in the arithmetic composition of values.

Today we will be having a look at 4 variants of Logarithmic functions in R programming:

1. log()
2. log1p()
3. log10()
4. log2()

Let us get started!!

## 1. R log() function

The `log() function` in R, returns the natural logarithmic value of the parameter passed to it. Moreover, these log values are calculated with respect to the base – e.

Let us have a look at the below example!

Example:

```x = 10
res_log = log(x)
print(res_log)
```

In the above example, we do not specify the base value for the function. It takes ‘e’ as the default value for base while executing the function.

Output:

``` 2.302585
```

On the other hand, we can specify the value of base by setting the value of the parameter ‘`base`‘ in the log() function as shown below.

```x = 10
res_log = log(x,base=10)
print(res_log)
```

Here, the log() function calculates the logarithmic values with respect to the base 10.

Output:

```1
```

## 2. R log1p() function

The function `log1p(y)` enables us to calculate the accurate natural logarithmic value of ‘`1+y`‘ for any entity passed to it. If we pass 10 as the value of y, it would calculate the natural log of 1+10 with the base ‘e’.

log1p does not give us an error with 0 values. In fact, it outputs 0. On the other side, if a negative value is passed to the function, NaN would be returned.

Have a look at the below case!

Example:

```res_log = log1p(10)
print(res_log)

res_log = log1p(0)
print(res_log)

res_log = log1p(-10)
print(res_log)
```

Output:

``` 2.397895
 0
Warning message:
In log1p(-10) : NaNs produced
 NaN
```

As you can see the results match what we mentioned before. The log1p function tries to evaluate the expressions or values to the decimal and gives an accurate representation.

## 3. R log10() function

R `log10()` function is an in-built function that calculates the natural logarithmic value of the entity considering ’10’ as the value of base for it.

The log10() function returns infinity for Zero and a NaN value for a negative entity being passed to it.

Example:

```res_log = log10(10)
print(res_log)

res_log = log10(-10)
print(res_log)

res_log = log10(0)
print(res_log)
```

Output:

``` 1
Warning message:
NaNs produced
 NaN
 -Inf
```

## 5. R log2() function

On the similar lines, `log2()` function calculates the natural logarithmic value for an entity with respect to ‘2’ as the value of base for evaluation.

Example:

```x = 100
res_log = log2(x)
print(res_log)
```

Output:

```6.643856
```

## Conclusion

By this, we have come to the end of this topic. In this article, we have studied the 4 IMP variants of Logarithm in R programming.

Try implementing these functions with various data values and do let us know about your understanding in the comment section.

For more such posts related to R programming, stay tuned and till then, Happy Learning!! 🙂

## References

close
Generic selectors
Exact matches only
Search in title
Search in content