[R] Changing sign on absolute numbers 0 problems

Brian Diggs diggsb at ohsu.edu
Wed Oct 20 22:27:51 CEST 2010


On 10/20/2010 12:25 PM, Ben Bolker wrote:
> Sadz A<sadz_a1000<at>  yahoo.co.uk>  writes:
>
>>
>> This works fine untill D=0
>> because then 'sign' does not give 0 a +ve sign it takes it as 0 and multiplies
>> decimal by 0 to give 0.
>> example
>> D<-0
>> decimal<-D+(M/60)+(S/3600)
>> decimal.degs<-sign(D)*decimal
>> decimal.degs
>> 0
>>
>
>    decimal.degs<- ifelse(D==0,1,sign(D))*decimal
>
>   work?

While Ben gives you what you asked for, I can see a pitfall that what 
you asked for is not what you really want.  In that notation, there is a 
difference between 0 degrees and -0 degrees.  However, as numbers, R 
would treat them as the same.

 > identical(0L,-0L)
[1] TRUE
 > identical(0,-0)
[1] TRUE

So that information gets lost.  Which really is another way of looking 
at the original question: when degrees are 0, the sign indicating 
east/west gets lost.  The "real" solution involves going back to the 
original (string?) form of D, and separating out the sign and the number 
as two different pieces of information.

-- 
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University



More information about the R-help mailing list