Types of R object – 3. complex numbers

All R objects have a class attribute, which can be important as R “decides” how to deal with objects based upon their class.

You can think of the simple classes as being in different categories:

  • Basic: numeric, character or factor
  • Logical: TRUE or FALSE
  • Complex: A number with real and imaginary parts

Object type: complex

You can make a complex number simply by appending an “imaginary” part to an actual number:

> newvec <- c(1+1i, 2+3i)
> newvec
[1] 1+1i 2+3i

So, R recognises 2+3i for example as a complex number with real part = 2, imaginary part = 3

The class() command shows that the result is complex:

> class(newvec)
[1] "complex"

There are several commands associated with complex numbers:

# The real part of the number
> Re(newvec)
[1] 1 2

# The imaginary part
> Im(newvec)
[1] 1 3

# The modulus
> Mod(newvec)
[1] 1.414214 3.605551

# The argument
> Arg(newvec)
[1] 0.7853982 0.9827937

# The complex conjugate
> Conj(newvec)
[1] 1-1i 2-3i

The commands help in dealing with complex numbers. In addition, the elementary trigonometric, logarithmic, exponential, square root and hyperbolic functions are implemented for complex values.

Comments are closed.