← Back to R Course | Chapter 2: Variables & Types | Lesson 6 of 7

Complex Type

In this page:

  1. Complex Type

Complex Type

R natively supports complex numbers with the complex type, written as a+bi using i for the imaginary unit, like 3+4i. Standard arithmetic operators work directly on complex numbers. This is less commonly used than the other basic types but is fully built in for scientific and engineering computation.

Note: Mod() computes a complex number's magnitude, and Re()/Im() extract its real and imaginary parts.

Example: Complex Type

markup
z <- 3 + 4i

cat(class(z), "\n")
cat("Magnitude:", Mod(z), "\n")
cat("Real part:", Re(z), "Imaginary part:", Im(z), "\n")
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.