rvectornanexponential

Why raising a vector of negative values to a fractional power gives NaN


If I generate a vector of values, some of which are negative:

d <- rnorm(1000, 4, 10)
z <- sapply(d, function(x) x^1.2)

z will contain a mix of real values and NaNs. Any values that were originally negative in the vector will be NaN.

Why is this? How can I fix it?


Solution

  • You should use complex numbers, like below

    (d + 0i)^1.2