How to Find the Cube Root in R [kader package]

When looking to do a cube root in r there is no base function for the task. However, there are three ways of finding a cube root. The first uses the cube root function in the kader package. The other two use logarithms and exponents to do the task. The one advantage that the cube root function Is that it is capable of handling negative numbers.

Description

The cube root function is a function that comes with the kader package, and it has the form of kader:::cuberoot(x) where x is the value the cube root is being taken of. While there are two other ways of obtaining a cube root this function is the only one that can handle negative numbers. The logarithms or exponents are native to r studio and so you do not have to add a package. If you are going to be finding the cube roots of negative numbers installing the kader package is the solution. Otherwise, the logarithm or exponent options may be the way you want to go.

Explanation

When doing a cube root in r remember that, unlike the square root function, r does not have a native cube root function. It has two other ways of taking cube roots and all three produce a perfect cube, but the native operations only do a digit transformation on positive numbers. As a result, if you want to work with negative numbers, it is necessary to use the kader cube root function. Naturally, this requires first installing the kader package. All three produce cube roots, but only the function from the kader package produces cube roots for negative numbers, but only the logarithmic and exponential operations can be used on a complex number.
<h2>Examples</h2> Here are several code examples of all three operations in use.

> x=27
> z=kader:::cuberoot(x)
> z
[1] 3
> z^3
[1] 27

Here is an example of the cube root function from the kader package. It works the same as the sqrt function does but it is not native to the r environment.

> x=27
> z=10^(log10(x)/3)
> z
[1] 3
> z^3
[1] 27
> z=x^(1/3)
> z
[1] 3
> z^3
[1] 27

This example illustrates both the logarithmic and exponential methods of producing cube roots.

> x = data.frame(a=c(1,2,3,4,5,6,7,8,9,-10),
+ b=c(3,6,9,12,15,18,21,24,27,30),
+ c=c(4,8,12,16,20,24,28,32,36,40))
> kader:::cuberoot(x)
a b c
1 1.000000 1.442250 1.587401
2 1.259921 1.817121 2.000000
3 1.442250 2.080084 2.289428
4 1.587401 2.289428 2.519842
5 1.709976 2.466212 2.714418
6 1.817121 2.620741 2.884499
7 1.912931 2.758924 3.036589
8 2.000000 2.884499 3.174802
9 2.080084 3.000000 3.301927
10 -2.154435 3.107233 3.419952

> 10^(log10(x)/3)
a b c
1 1.000000 1.442250 1.587401
2 1.259921 1.817121 2.000000
3 1.442250 2.080084 2.289428
4 1.587401 2.289428 2.519842
5 1.709976 2.466212 2.714418
6 1.817121 2.620741 2.884499
7 1.912931 2.758924 3.036589
8 2.000000 2.884499 3.174802
9 2.080084 3.000000 3.301927
10 NaN 3.107233 3.419952

> x^(1/3)
a b c
1 1.000000 1.442250 1.587401
2 1.259921 1.817121 2.000000
3 1.442250 2.080084 2.289428
4 1.587401 2.289428 2.519842
5 1.709976 2.466212 2.714418
6 1.817121 2.620741 2.884499
7 1.912931 2.758924 3.036589
8 2.000000 2.884499 3.174802
9 2.080084 3.000000 3.301927
10 NaN 3.107233 3.419952

In this example, we illustrate how all three methods work on a data frame. A negative number has been included to show how these operations behave. Note that the logarithmic and exponential methods produce NaN values for the negative number.

Application

There are several applications of cube root in r. For example, if you were given the volumes of various planetary bodies and needed to calculate the radius of each one, you will need to use cube roots. In fact, this would be the case for any spherical object that you may be working with.

When doing a cube root in r, the cube root function, logarithmic function, and exponent operator are all useful tools. The one you use will depend upon the situation. The cube root function from the kader package works with negative numbers, while the logarithmic function and exponent operator work on complex numbers. Which one you use will depend upon the numbers you are working with.

Scroll to top
Privacy Policy