Search code examples
rif-statementcoordinate

How to avoid error ocuured due to minus sign of southern latitudes when mixed set of latitudes convert from degrees minutes to decimal degree using R


I have mixed data set of both North (positive) and Negative latitude column and separate minutes column in data frame. I used simple Lat + (minutes/60) to convert this in to decimal degrees.

lat1 <- c(7, -7, 6,  -0, -1, 6,  8, -7, 6,  6)
lat2 <- c(7.4, 55.7, 32.6,  8.9, 47.5, 25.6,  6.8, 45.7, 24.6,  7.6)

ifelse(lat1<0,(lat <- lat1-(lat2/60)),(lat <- lat1+(lat2/60)))

>[1]  7.1233333 -7.9283333  6.5433333  0.1483333 -1.7916667  6.4266667
 [7]  8.1133333 -7.7616667  6.4100000  6.1266667

this result is correct but

> lat
 [1]  7.1233333 -6.0716667  6.5433333  0.1483333 -0.2083333  6.4266667
 [7]  8.1133333 -6.2383333  6.4100000  6.1266667

ifelse statement provide correct result in R console but not to stored it to vector "lat" I need to add minutes/60 to degree if degree value is positive and subtract minutes/60 from degrees if degree value is negative


Solution

  • The correct syntax to use ifelse here is the following:

    lat <-  ifelse(lat1 < 0, lat1 - lat2 / 60, lat1 +  lat2 / 60)
    

    ifelse returns a vector where component i = lat1[i] - lat2[i] / 60 if lat[i] < 0 and lat[1] + lat2[i] / 60 otherwise so you don't need to put the assignment inside.