R Complex Data Type

The complex data type stores numbers that have two parts: a real part and an imaginary part. Complex numbers appear in mathematics, electrical engineering, signal processing, and advanced statistics. Most everyday data analysis does not require them, but R supports them natively.

What Is a Complex Number?

A complex number looks like: a + bi
─────────────────────────────────────────────
  a  = real part (a regular number)
  b  = imaginary part (multiplied by i)
  i  = imaginary unit (√-1)

Examples:
  3 + 2i    →  real: 3,   imaginary: 2
  -1 + 0i   →  real: -1,  imaginary: 0
  0 + 5i    →  real: 0,   imaginary: 5 (purely imaginary)

Creating Complex Variables in R

z1 <- 3 + 2i
z2 <- complex(real = 5, imaginary = -3)
z3 <- 4i          # shorthand for 0 + 4i

class(z1)         # "complex"
class(2i)         # "complex"

Extracting Parts of a Complex Number

z <- 6 + 4i

Re(z)    # 6    — real part
Im(z)    # 4    — imaginary part
Mod(z)   # 7.211... — magnitude (distance from origin)
Conj(z)  # 6 - 4i  — complex conjugate (flip sign of imaginary part)
Arg(z)   # 0.5880... — angle in radians

Visual: Complex Number on a Plane

Imaginary axis
      ▲
   4  │    • z = 6 + 4i
      │   /
      │  / Mod(z) = √(6²+4²) = 7.21
      │ /
      │/ Arg(z)
──────┼────────────► Real axis
      │         6

Arithmetic with Complex Numbers

a <- 3 + 2i
b <- 1 + 4i

a + b   # (3+1) + (2+4)i = 4 + 6i
a - b   # (3-1) + (2-4)i = 2 - 2i
a * b   # 3*1 + 3*4i + 2i*1 + 2i*4i
        # = 3 + 12i + 2i + 8i²
        # = 3 + 14i + 8(-1) = -5 + 14i
a / b   # (-5 + 10i) / 17 = -0.294 + 0.588i

Common Complex Functions

Function        Description                   Example
──────────────────────────────────────────────────────────
Re(z)           Real part                     Re(3+2i) = 3
Im(z)           Imaginary part                Im(3+2i) = 2
Mod(z)          Magnitude (absolute value)    Mod(3+4i) = 5
Conj(z)         Complex conjugate             Conj(3+2i) = 3-2i
sqrt(-1+0i)     Square root of complex        0+1i
exp(1i * pi)    Euler's formula               -1+0i (approximately)

Checking and Converting Complex

is.complex(3 + 2i)   # TRUE
is.complex(5)        # FALSE

as.complex(7)        # 7+0i  — converts numeric to complex
as.numeric(3+0i)     # 3     — works only if imaginary part is 0

A Practical Example: Signal Frequency

In electrical engineering, alternating current circuits use complex numbers to represent signals. Here is a simplified example:

# Impedance in an AC circuit
resistance    <- 50       # ohms (real part)
reactance     <- 30       # ohms (imaginary part)
impedance     <- complex(real = resistance, imaginary = reactance)

cat("Impedance:", impedance, "\n")
cat("Magnitude:", round(Mod(impedance), 2), "ohms\n")

Output:

Impedance: 50+30i
Magnitude: 58.31 ohms

When You Will Encounter Complex Numbers in R

  • Fourier transforms for signal analysis
  • Eigenvalue computations in linear algebra
  • Square roots of negative numbers (R returns complex, not NaN)
  • Advanced time series analysis
sqrt(-4)        # NaN with warning (numeric input)
sqrt(-4 + 0i)   # 0+2i (complex input gives complex result)

Complex numbers are a niche data type in R. Beginners rarely need them directly. However, knowing they exist helps you understand certain R outputs and error messages, especially when working with mathematical or engineering datasets.

Leave a Comment

Your email address will not be published. Required fields are marked *