Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I have a dataset that has latitude and longitude listed as follows under the column name location_1:

   (34.0545, -118.2499)

See I can remove the parenthesis and can able to move them into separate columns under normal circumstances, but I don't know how to make it work while dropping the first comma before the numbers.

1 Answer

0 votes
by (108k points)

I think this should work with the tidyr::extract, following with the regex().

tidyr::extract(df, location_1, c('lat', 'lon'), 

                  regex = ',?\\s*\\((\\d+\\.\\d+).*(-?\\d+\\.\\d+)\\)')

If you are a beginner in R then do check out the R programming tutorial.

Browse Categories

...