[R] as.complex

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Oct 7 08:58:50 CEST 2004


On Thu, 7 Oct 2004, Erich Neuwirth wrote:

> as.complex("2+1i") -> 2+1i
> as.complex("2+i") -> NA
> 
> Does somebody have a modified version of as.complex
> which does the coercion in a less strict manner and
> produces a complex number also for strings like the
> second example?
> 
> Perhaps it would even make sense to change the behavior
> of as.complex to handle the second case.
> After all, this is an established way of writing
> complex numbers in some programming tools
> able to deal with complex numbers.

R does document that 1i is required, and as.complex uses the same rules 
for entering complex numbers as e.g. using them as part of an arithmetic 
expression.  Consistency is very important in a programming language.

It is easy to change 2+i to 2+1i using sub(), for example.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list