Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaning Geocode Data

I've got a df just like this:

df = data.frame(longitude = c('-235.969', 
                       '-23.596.244', 
                       '-2.359.186'))

It´s an example of one column of geocode I'm trying to convert to something like this

new_df = data.frame(longitude = c('-23.5969', '-23.596244', '-23.59186'))

The main purpose is to use the geocode in an leaflet application.

like image 620
Rafael Bicudo Avatar asked Apr 29 '26 11:04

Rafael Bicudo


1 Answers

If really necessary, I would do this in two steps:

library(magrittr)
gsub(".", "", df$longitude, fixed = TRUE) %>%
  sub("(\\d{2})", "\\1\\.", .)

[1] "-23.5969"   "-23.596244" "-23.59186" 

First drop any . then replace the first two digits with the first two digits + .

PS. without pipes you could do:

sub("(\\d{2})", "\\1\\.", gsub(".", "", df$longitude, fixed = TRUE))

EDIT: Important caveat:

As Matt points out this only works if your longitude ALWAYS consist of if your longitude degree is two digits (10-99).

like image 143
sindri_baldur Avatar answered May 02 '26 04:05

sindri_baldur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!