Roy Smith
Roy Smith

Reputation: 2163

Real part of complex number?

I'm just learning R, so please forgive what I'm sure is a very elementary question. How do I take the real part of a complex number?

Upvotes: 22

Views: 28476

Answers (2)

flodel
flodel

Reputation: 89057

If you read the help file for complex (?complex), you will see a number of functions for performing complex arithmetic. The details clearly state

The functions Re, Im, Mod, Arg and Conj have their usual interpretation as returning the real part, imaginary part, modulus, argument and complex conjugate for complex values.

Therefore

Use Re:

Re(1+2i)
# 1

For the imaginary part:

Im(1+2i)
# 2

?complex will list other useful functions.

Upvotes: 26

sehe
sehe

Reputation: 392921

Re(z)

and

Im(z)

would be the functions you're looking for.

See also http://www.johnmyleswhite.com/notebook/2009/12/18/using-complex-numbers-in-r/

Upvotes: 7

Related Questions