Hi, I want to apply ^ operator to a vector but it is applied to some of the elements correctly and to some others, it generates NaN. Why is it not able to calculate -6.108576e-05^(1/3) even though it exists? tmp [1] -6.108576e-05 4.208762e-05 3.547092e-05 7.171101e-04 -1.600269e-03> tmp^(1/3)[1] NaN 0.03478442 0.03285672 0.08950802 NaN> -6.108576e-05^(1/3)[1] -0.03938341
Hi AFAIK, this is issue of the preference of operators. r-help-bounces at r-project.org napsal dne 16.11.2009 11:24:59:> Hi, > I want to apply ^ operator to a vector but it is applied to some of the > elements correctly and to some others, it generates NaN. Why is it notable to> calculate -6.108576e-05^(1/3) even though it exists? > > > tmp > [1] -6.108576e-05 4.208762e-05 3.547092e-05 7.171101e-04-1.600269e-03> > tmp^(1/3) > [1] NaN 0.03478442 0.03285672 0.08950802 NaNThis computes (-a)^(1/3) which is not possible in real numbers. You have to use as.complex(tmp)^(1/3) to get a result.> > -6.108576e-05^(1/3) > [1] -0.03938341this is actually -(6.108576e-05^(1/3)) Regards Petr> > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
R does not "know" that 1/3 is 1/3. It is represented internally as 0.33333...3, so certain mathematical facts such as the existence of real roots of fractional integer powers are opaque to R (since it is not a symbolic algebra system.) Try seaarching for cube roots on R-search (for example): http://finzi.psych.upenn.edu/Rhelp08/2009-July/205006.html # Further comments by Ted Harding > complex(real=-6.108576e-05)^(1/3) [1] 0.01969171+0.03410703i' So the safest way to get the real cube root (as opposed the complex roots) is to use: sign(tmp)*abs(tmp)^1/3 > sign(tmp)*abs(tmp)^(1/3) [1] -0.03938341 0.03478442 0.03285672 0.08950802 -0.11696726 On Nov 16, 2009, at 5:24 AM, carol white wrote:> Hi, > I want to apply ^ operator to a vector but it is applied to some of > the elements correctly and to some others, it generates NaN. Why is > it not able to calculate -6.108576e-05^(1/3) even though it exists? > > > tmp > [1] -6.108576e-05 4.208762e-05 3.547092e-05 7.171101e-04 > -1.600269e-03 >> tmp^(1/3) > [1] NaN 0.03478442 0.03285672 0.08950802 NaN >> -6.108576e-05^(1/3) > [1] -0.03938341 >-- David Winsemius, MD Heritage Laboratories West Hartford, CT