How to find the cube root of negative values in R?

R ProgrammingServer Side ProgrammingProgramming

There is no function in R to find the cube root of negative values, hence we need to create that. The code to create the function is as shown below −

CubeRoot<-function(x){
   sign(x)*abs(x)^(1/3)
}

Now we just need to pass the values in the function to find the cube root of those values. These values can be a vector or a single value as well.

Examples of finding the cube roots using our function CubeRoot −

CubeRoot(-3)

[1] -1.44225

CubeRoot(-9)

[1] -2.080084

CubeRoot(-27)

[1] -3

CubeRoot(-125)

[1] -5

CubeRoot(-1000)

[1] -10

CubeRoot(-64)

[1] -4

CubeRoot(-1)

[1] -1

CubeRoot(-81)

[1] -4.326749

CubeRoot(-216)

[1] -6

CubeRoot(c(-1,-9,-64,-125,-216))

[1] -1.000000 -2.080084 -4.000000 -5.000000 -6.000000

x1<-c(-8,-100,-500,-1000,-5000,-10000)

CubeRoot(x1)

[1] -2.000000 -4.641589 -7.937005 -10.000000 -17.099759 -21.544347

Example

 Live Demo

x2<-sample(-10:-1,100,replace=TRUE)
x2

Output

[1] -3 -1 -1 -4 -10 -7 -7 -2 -4 -8 -1 -9 -8 -6 -9 -5 -6 -5
[19] -2 -8 -1 -8 -5 -9 -3 -7 -9 -10 -3 -2 -1 -9 -1 -8 -8 -6
[37] -5 -4 -4 -2 -3 -3 -2 -4 -8 -7 -5 -7 -7 -6 -1 -5 -5 -6
[55] -7 -3 -1 -6 -1 -2 -2 -1 -5 -7 -3 -4 -7 -9 -7 -1 -10 -4
[73] -8 -8 -5 -8 -7 -3 -4 -5 -10 -10 -2 -3 -7 -8 -8 -2 -8 -5
[91] -2 -1 -5 -10 -5 -4 -1 -3 -4 -6

Example

CubeRoot(x2)

Output

[1] -2.154435 -2.154435 -1.259921 -1.817121 -1.709976 -1.259921 -1.000000 [8] -2.000000 -1.912931 -2.000000 -1.587401 -2.080084 -1.000000 -1.817121 [15] -2.154435 -1.587401 -1.000000 -1.817121 -1.587401 -1.259921 -1.000000 [22] -1.709976 -1.709976 -1.912931 -1.259921 -1.912931 -2.080084 -2.000000 [29] -1.587401 -2.154435 -1.259921 -1.912931 -2.080084 -1.442250 -2.000000 [36] -1.000000 -1.000000 -1.817121 -1.912931 -2.000000 -1.442250 -1.587401 [43] -1.259921 -2.154435 -1.817121 -2.080084 -1.259921 -1.587401 -2.154435 [50] -1.442250 -1.259921 -1.709976 -1.912931 -1.259921 -2.000000 -2.000000 [57] -1.817121 -1.442250 -1.587401 -1.587401 -2.000000 -2.080084 -1.587401 [64] -1.442250 -1.817121 -1.442250 -1.442250 -1.587401 -1.259921 -1.000000 [71] -1.709976 -1.442250 -1.587401 -1.587401 -2.000000 -1.709976 -1.709976 [78] -2.080084 -1.912931 -2.000000 -2.000000 -1.259921 -1.442250 -1.912931 [85] -1.912931 -1.912931 -1.259921 -2.154435 -2.080084 -2.080084 -1.259921 [92] -1.912931 -1.259921 -1.259921 -1.259921 -2.080084 -2.000000 -1.259921 [99] -2.000000 -2.000000

Example

 Live Demo

x3<-sample(-100:-1,50,replace=TRUE)
x3

Output

[1] -5 -57 -31 -21 -9 -47 -13 -27 -59 -86 -94 -8 -41 -37 -5
[16] -88 -15 -75 -24 -52 -59 -100 -8 -51 -57 -61 -91 -46 -56 -24
[31] -62 -95 -17 -22 -83 -83 -37 -37 -52 -49 -29 -9 -67 -88 -100
[46] -15 -81 -32 -88 -48

Example

CubeRoot(x3)

Output

[1] -2.620741 -3.848501 -4.562903 -3.732511 -3.870877 -3.583048 -4.290840 [8] -3.419952 -3.174802 -2.000000 -3.779763 -4.121285 -2.620741 -4.272659 [15] -3.476027 -3.503398 -3.072317 -1.000000 -4.235824 -4.217163 -4.121285 [22] -3.556893 -4.235824 -4.061548 -1.442250 -3.892996 -3.756286 -3.239612 [29] -4.081655 -4.431048 -4.530655 -4.041240 -3.448217 -2.289428 -4.101566 [36] -4.272659 -2.000000 -3.914868 -4.326749 -4.578857 -4.160168 -2.289428 [43] -3.608826 -3.448217 -3.556893 -2.000000 -3.107233 -2.080084 -3.271066 [50] -2.410142

Example

 Live Demo

x4<-sample(-1000:-1,50)
x4

Output

[1] -466 -806 -821 -759 -685 -503 -271 -340 -170 -437 -667 -943 -84 -78 -846
[16] -957 -383 -776 -351 -473 -77 -508 -958 -655 -408 -725 -773 -780 -738 -575
[31] -751 -240 -696 -154 -390 -823 -488 -516 -787 -450 -795 -775 -676 -724 -803
[46] -783 -566 -829 -645 -249

Example

CubeRoot(x4)

Output

[1] -7.146569 -9.461525 -3.914868 -7.080699 -9.483814 -8.987637 -9.739963 [8] -3.239612 -7.947574 -8.680124 -5.981424 -9.065368 -6.205822 -7.894447 [15] -8.836556 -7.747311 -4.121285 -5.838272 -6.634287 -9.420387 -8.173302 [22] -8.719760 -6.965820 -8.900130 -9.582840 -9.827025 -8.358678 -1.259921 [29] -7.067377 -6.153449 -9.809736 -8.545317 -9.959839 -9.868272 -4.379519 [36] -8.193213 -3.000000 -9.468966 -5.528775 -7.100588 -4.702669 -5.167649 [43] -5.604079 -9.561011 -9.888767 -3.659306 -5.828477 -8.879040 -6.656930 [50] -8.962809

Example

 Live Demo

x5<-sample(-999:-100,50)
x5

Output

[1] -425 -563 -342 -776 -860 -985 -263 -871 -350 -845 -426 -509 -511 -706 -672
[16] -334 -789 -843 -372 -535 -247 -495 -249 -326 -476 -687 -365 -680 -549 -299
[31] -893 -628 -638 -284 -368 -886 -758 -167 -360 -553 -347 -128 -123 -681 -280
[46] -419 -108 -925 -763 -314

Example

CubeRoot(x5)

Output

[1] -9.405339 -5.217103 -9.321698 -6.526519 -7.791488 -8.767719 -5.838272 [8] -6.307994 -5.828477 -8.153294 -7.984344 -9.181500 -9.976612 -9.531750 [15] -6.000000 -9.830476 -9.953114 -7.968627 -5.667051 -7.524365 -6.875344 [22] -6.811285 -8.305865 -8.051748 -8.604252 -7.386437 -9.672740 -9.256022 [29] -6.626705 -8.971101 -5.383213 -9.542744 -9.267680 -8.372967 -7.434994 [36] -6.723951 -9.679860 -6.349604 -9.340839 -9.929504 -8.429638 -8.874810 [43] -8.173302 -7.299894 -6.179747 -7.736188 -7.963374 -8.715373 -4.747459 [50] -4.959676
raja
Published on 10-Oct-2020 15:24:44
Advertisements