0

I have large spatial data set in text. i need to convert degree minute.decimal values to decimal degrees. my code as follows. it works well values except 0 to -1. values in 0 to -1 convert to positive values by as.numeric() Coordinate conversion method is fine, but problem is how to pass minus zero (-0) values in iflese statement. how i overcome this problem?

data <- c("-7ø12.2","-1ø23.0","-0ø50.3","0ø14.6","2ø59.7")
# latitudes in degree minutes.decimal format
lat1 <- as.numeric(gsub( "ø.*$", "", data)) # substring befor ø
lat2 <- as.numeric(sub(".*ø", "", data)) # substring after ø
lat <-  ifelse(lat1 < 0, lat1 - lat2 / 60, lat1 +  lat2 / 60) 

lat1 [1] -7 -1 0 0 2

sudheera
  • 53
  • 10
  • This is a potential duplicate to [Converting geo coordinates from degree to decimal](http://stackoverflow.com/questions/14404596/converting-geo-coordinates-from-degree-to-decimal) – Uwe Aug 01 '16 at 07:55
  • @UweBlock Here question is not the conversion from degree to decimal, but avoid minus zero values (0 to -1) to being positive in ifelse statement. – sudheera Aug 02 '16 at 08:44

1 Answers1

0

You are testing for lat1 < 0 but lat1[3] is 0 so the test fails, and your ifelse hits the addition result. You appear to be hoping that -0 stays negative but that's not how 0 works.

Add another extraction (the negative sign):

neg <- substr(data, 1, 1)
neg
#> [1] "-" "-" "-" "0" "2"

and test on that instead of lat1 < 0

ifelse(neg == "-", lat1 - lat2 / 60, lat1 +  lat2 / 60) 
#> [1] -7.2033333 -1.3833333 -0.8383333  0.2433333  2.9950000
Jonathan Carroll
  • 3,897
  • 14
  • 34