# How to calculate cube root in r

The cube root is a commonly used mathematical function. When doing data processing, sometimes finding the cube roots of values can come in handy. This is particularly the case when working with volumes.

### Three Ways to Find the Cube Root of a Number in R

R Programming offers three Ways of finding the cube root of a number. The first approach is to simply raise the number to the one-third power. The second approach takes the natural log of the value and divides it by three, before raising e to the result. Both of these approaches will produce NaN values for negative numbers. Consequently, working with negative numbers requires using the sign and absolute value functions to get the correct result. The third option uses the nthroot from the pracma package, and automatically works with negative numbers.

### Preventing a Common Error – Logarithms & Absolute Value

The reason why the cube root and natural algorithm produce NaN values is that they both work by using logarithms, and you do not get a real number when taking a logarithm of a negative number. You fixed this problem by taking the absolute value before running the logarithm and then adding the appropriate sign. This allows you to take the cube root of a negative number.

Finding the Cube Root in R (3 Examples)

Here are three examples showing different ways of obtaining a square root. They each process a vector of ten random numbers to demonstrate a variety of numbers.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = as.integer(rnorm(10)*10)
> x
[1] -27 11 -7 -12 11 2 5 5 4 -8
> y = sign(x)*abs(x)^(1/3)
> y
[1] -3.000000 2.223980 -1.912931 -2.289428 2.223980 1.259921
[7] 1.709976 1.709976 1.587401 -2.000000

This example simply takes the one-third power of the values being processed. The additional functions allow negative numbers to also be processed.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = as.integer(rnorm(10)*10)
> x
[1] 7 -21 -3 8 3 -6 3 -13 10 -1
> y = sign(x)*exp(log(abs(x))/3)
> y
[1] 1.912931 -2.758924 -1.442250 2.000000 1.442250 -1.817121
[7] 1.442250 -2.351335 2.154435 -1.000000

In this example, we take the natural log of the value being processed and then divide it by three before raising e to the power of the result. The extra functions are for working with negative numbers.

> library(pracma)
> t = as.numeric(Sys.time())
> set.seed(t)
> x = as.integer(rnorm(10)*10)
> x
[1] 19 10 9 -4 12 9 8 -5 9 -8
> y = nthroot(x,3)
> y
[1] 2.668402 2.154435 2.080084 -1.587401 2.289428 2.080084
[7] 2.000000 -1.709976 2.080084 -2.000000

In this example, we use the nthroot function from the Pracma package. This function has the built-in ability to work with negative numbers.

### Applications – Examples of Programs where you need to Find the Cube Root in R?

One application of finding the cube root of a number is finding the average length, height, or width of a group of objects for which you know the volumes. Another application would be flattening out a curve when creating a data model. This can make producing a model a lot easier.

The cube root of a number can be obtained in R programming in at least three different ways. All three of them have their advantages, with the first two being based on mathematical functions that you are probably already familiar with, and the third being specifically made to do the job in R programming.

Scroll to top