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
But,
extract
looks cleaner for this since you can remove the "()" at the same timeYou can use
base R
to do this. Remove the parentheses withgsub
and useread.table
to read the column 'x' (based on @jazzuro's example) to separate into two columns.Alternatively, you could take numbers and create a data frame using the stringi package.
Data