I have a dataset with a column of locations like this (41.797634883, -87.708426986). I'm trying to split it into latitude and longitude. I tried using the separate method from the tidyr package
library(dplyr)
library(tidyr)
df <- data.frame(x = c('(4, 9)', '(9, 10)', '(20, 100)', '(100, 200)'))
df %>% separate(x, c('Latitude', 'Longitude'))
but I'm getting this error
Error: Values not split into 2 pieces at 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
What am I doing wrong?
Specify the separating character
dataframe %>% separate(Location, c('Latitude', 'Longitude'), sep=",")
But, extract
looks cleaner for this since you can remove the "()" at the same time
dataframe %>% extract(x, c("Latitude", "Longitude"), "\\(([^,]+), ([^)]+)\\)")
You can use base R
to do this. Remove the parentheses with gsub
and use read.table
to read the column 'x' (based on @jazzuro's example) to separate into two columns.
read.table(text=gsub('[()]', '', mydf$x),
sep=",", col.names=c('Latitute', 'Longitude'))
# Latitute Longitude
#1 41.79763 -87.70843
#2 41.91139 -87.73264
#3 41.67293 -87.64282
#4 41.75993 -87.69887
#5 41.85612 -87.71745
#6 41.90079 -87.67124
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With