How to find the fractional power of a negative number in R?

R ProgrammingServer Side ProgrammingProgramming

To find the fractional power of a negative number, we can find the power separately for the numerator and denominator where denominator will have the numerator 1. For example, if we have a vector called x that contains a single value -10 then the fractional power 15/7 of x can be found by using the command ((x)^15)^(1/7)

Example

 Live Demo

x1<-sample(-100:-1,120,replace=TRUE)
x1

Output

  [1] -82 -21 -92 -25 -80 -71 -62 -100 -21 -65 -72 -25 -12 -95 -40
 [16] -58 -1 -50 -63 -36 -81 -67 -98 -87 -41 -99 -83 -34 -35 -33
 [31] -68 -56 -96 -15 -80 -53 -32 -33 -29 -14 -16 -31 -42 -30 -22
 [46] -100 -61 -9 -6 -41 -40 -54 -91 -19 -73 -92 -39 -55 -34 -61
 [61] -3 -64 -17 -94 -49 -31 -61 -6 -92 -19 -46 -23 -21 -15 -12
 [76] -94 -55 -71 -85 -1 -32 -11 -99 -89 -50 -31 -81 -15 -11 -41
 [91] -8 -18 -100 -78 -4 -82 -28 -74 -18 -71 -36 -46 -88 -88 -46
[106] -66 -97 -59 -38 -37 -38 -70 -85 -86 -74 -65 -21 -26 -88 -53

Finding 4/3 power of x1 −

Example

(x1)^(4/3)

Output

  [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
 [19] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
 [37] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
 [55] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
 [73] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
 [91] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
[109] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

We need to do it separately as shown below −

Example

((x1)^4)^(1/3)

Output

[1] 356.247482 57.937408 415.320884 73.100443 344.709550 293.998060
[7] 245.389280 464.158883 57.937408 261.347174 299.532071 73.100443
[13] 27.473142 433.475750 136.798076 224.510845 1.000000 184.201575
[19] 250.680604 118.869381 350.466646 272.123723 451.822757 385.501143
[25] 141.376907 457.980436 362.051866 110.146801 114.487321 105.848633
[31] 277.552547 214.248292 439.570269 36.993181 344.709550 199.083145
[37] 101.593667 105.848633 89.097188 33.741992 40.317474 97.382800
[43] 145.993119 93.216975 61.644865 464.158883 240.126328 18.720754
[49] 10.902724 141.376907 136.798076 204.107210 409.312672 50.699631
[55] 305.091761 415.320884 132.257246 209.162385 110.146801 240.126328
[61] 4.326749 256.000000 43.711787 427.402579 179.305980 97.382800
[67] 240.126328 10.902724 415.320884 50.699631 164.820202 65.408941
[73] 57.937408 36.993181 27.473142 427.402579 209.162385 293.998060
[79] 373.730522 1.000000 101.593667 24.463781 457.980436 397.362314
[85] 184.201575 97.382800 350.466646 36.993181 24.463781 141.376907
[91] 16.000000 47.173345 464.158883 333.267377 6.349604 356.247482
[97] 85.024491 310.676898 47.173345 293.998060 118.869381 164.820202
[103] 391.420496 391.420496 164.820202 266.721841 445.685987 229.686789
[109] 127.755065 123.292209 127.755065 288.489971 373.730522 379.604427
[115] 310.676898 261.347174 57.937408 77.024898 391.420496 199.083145

Example

 Live Demo

x2<-sample(-9999:-1000,100)
x2

Output

[1] -7272 -5751 -1636 -2472 -9926 -4662 -7875 -8298 -9609 -9856 -6251 -8216
[13] -7806 -2575 -1495 -8980 -8694 -8146 -6982 -4808 -1864 -5057 -7322 -7320
[25] -9551 -3311 -7527 -6362 -6404 -3281 -3795 -1596 -6568 -8763 -2750 -9087
[37] -2457 -9934 -2326 -2495 -4279 -2239 -1780 -5694 -2196 -8916 -5816 -2825
[49] -2693 -2320 -2830 -6863 -2914 -8724 -7639 -9850 -4919 -9801 -2190 -5013
[61] -1044 -7143 -5841 -6071 -2705 -5759 -2024 -6232 -6933 -1355 -2759 -1979
[73] -4591 -2808 -4290 -7028 -8762 -4642 -2075 -4539 -4127 -2908 -3615 -5029
[85] -3631 -1034 -5226 -7406 -1513 -8858 -7961 -1134 -8191 -3918 -2715 -6963
[97] -2845 -6121 -1129 -3485

Example

((x2)^2)^(1/3)

Output

[1] 375.3496 320.9930 138.8424 182.8236 461.8662 279.0711 395.8224 409.8727
[9] 451.9796 459.6922 339.3384 407.1680 393.5069 187.8674 130.7457 432.0336
[17] 422.8111 404.8520 365.3030 284.8675 151.4598 294.6198 377.0681 376.9995
[25] 450.1590 222.1465 384.0737 343.3437 344.8532 220.8026 243.2995 136.5700
[33] 350.7159 425.0453 196.2856 435.4587 182.0833 462.1143 175.5523 183.9559
[41] 263.5693 171.1470 146.8746 318.8685 168.9487 429.9785 323.4071 199.8384
[49] 193.5638 175.2503 200.0741 361.1403 204.0139 423.7832 387.8743 459.5056
[57] 289.2352 457.9804 168.6408 292.9084 102.9122 370.8974 324.3332 332.7925
[65] 194.1384 321.2906 160.0075 338.6504 363.5918 122.4502 196.7136 157.6270
[73] 276.2304 199.0359 264.0208 366.9057 425.0130 278.2724 162.6842 274.1407
[81] 257.2900 203.7337 235.5441 293.5313 236.2386 102.2540 301.1477 379.9465
[89] 131.7931 428.1117 398.6989 108.7449 406.3416 248.5286 194.6166 364.6400
[97] 200.7805 334.6172 108.4250 229.8627

Example

 Live Demo

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

Output

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

Example

((x3)^4)^(1/3)

Output

[1] 4.326749 10.902724 1.000000 2.519842 10.902724 4.326749 8.549880
[8] 1.000000 2.519842 6.349604 18.720754 2.519842 13.390518 10.902724
[15] 4.326749 4.326749 16.000000 1.000000 6.349604 1.000000 16.000000
[22] 18.720754 21.544347 13.390518 21.544347 1.000000 16.000000 2.519842
[29] 2.519842 2.519842 18.720754 13.390518 1.000000 18.720754 18.720754
[36] 8.549880 21.544347 16.000000 8.549880 18.720754 4.326749 8.549880
[43] 18.720754 16.000000 18.720754 18.720754 18.720754 16.000000 16.000000
[50] 21.544347 4.326749 6.349604 4.326749 6.349604 8.549880 10.902724
[57] 21.544347 1.000000 8.549880 13.390518 13.390518 2.519842 16.000000
[64] 4.326749 10.902724 4.326749 13.390518 16.000000 16.000000 13.390518
[71] 21.544347 16.000000 18.720754 2.519842 6.349604 8.549880 2.519842
[78] 2.519842 16.000000 6.349604 6.349604 13.390518 18.720754 8.549880
[85] 1.000000 13.390518 2.519842 4.326749 21.544347 16.000000 13.390518
[92] 4.326749 10.902724 6.349604 18.720754 18.720754 8.549880 1.000000
[99] 21.544347 2.519842
raja
Published on 17-Mar-2021 07:33:33
Advertisements