An R Introduction to Statistics

Complex

A complex value in R is defined via the pure imaginary value i.

> z = 1 + 2i     # create a complex number 
> z              # print the value of z 
[1] 1+2i 
> class(z)       # print the class name of z 
[1] "complex"

The following gives an error as 1 is not a complex value.

> sqrt(1)       # square root of 
[1] NaN 
Warning message: 
In sqrt(1) : NaNs produced

Instead, we have to use the complex value 1 + 0i.

> sqrt(1+0i)    # square root of 1+0i 
[1] 0+1i

An alternative is to coerce 1 into a complex value.

> sqrt(as.complex(1)) 
[1] 0+1i