Reputation: 1700
I have a dataset in R called Test with longitude latitude coordinates that now have the following format after having called str(Test):
$ lat : chr "513.035.763.793.677"
$ lon : chr "33.941.883.804.984"
I would like to turn these into correct lon - lat coordinates. The following doesn't work:
as.numeric(Test$lat)
as.numeric(gsub(".", "", Test$lat)
I get:
[1] NA Warning message: NAs introduced by coercion
How can I transform them into numeric coordinates that are good to plot on a map? the correct coordinates should be lon: 3.394188 and lat: 51.30357
Upvotes: 1
Views: 3487
Reputation: 3290
Try:
as.numeric(gsub(".", "", Test$lat, fixed = TRUE))
[1] 5.130358e+14
Without setting fixed = TRUE
, gsub
was treating "."
as a regular expression.
To then get it to the format you were looking for, you simply need to multply by 1e-13
:
5.130358e+14 * 1e-13
[1] 51.30358
Upvotes: 2