[R] Changing sign on absolute numbers 0 problems

David Winsemius dwinsemius at comcast.net
Wed Oct 20 21:19:27 CEST 2010


On Oct 20, 2010, at 11:47 AM, Sadz A wrote:

> Hi,
>
> I am trying to do some calculations turning DMS data to decimal  
> degrees using
> the formula (D+(M/60)+(S/3600)), some of the D's involve -ve  
> numbers, the
> easiest way to do the calculation is to use absolute numbers then  
> use the 'sign'
> function in R to change the answer back to the correct -ve or +ve,
>
> for example, if;
> D<--69
> M<-8
> S<-10
> then
> decimal<-D+(M/60)+(S/3600)
> -69+(8/60)+(10/3600)   = this would equal -68.86389  (which is  
> wrong, it should
> be -69.13611,  so i used the following function)
>
> decimal<-(abs(D)+(M/60)+(S/3600))
> decimal.degs<-sign(D)*decimal
> decimal.degs
> -69.13611
>
> because ((69+(8/60)+(10/3600)=69.13611) and then the -sign is put  
> back in.
>
> 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
>
> Is there anyway to get around this??????????

I am not sure I understand the source of your distress. Why are you  
disturbed that 0 returns 0? ??????

> and make D=0 a positive and not a 0 with sign or another  
> function??????

You can always define your own function:

nonneg <- function(x) { 0 + !sign(x) }

 > nonneg(0)
[1] 1


>
> Any help is appreciated
> Thank you
> sadz
>
> ps please email me if you need more info
-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list