Reputation: 307
A dataset (CSV) file has a column latitudes and longitudes as (33, -118), (34, -119), (36, -120) etc. There are a million rows.
How can I seperate them, for instance how can I create two new columns with lat and long separate values. I know how to map using ggmap given lat and long in separate columns.
Thank you for your help
Upvotes: 0
Views: 126
Reputation: 1860
One more attempt:
library(tidyverse)
df <- data.frame(col = c('(33, -118)', '(34, -119)', '(36, -120)'))
df %>%
mutate(col =col %>% str_sub(2,-2)) %>% # remove ( and )
separate(col, c('lat', 'lon'), convert=T) # separate
lat lon
1 33 118
2 34 119
3 36 120
Upvotes: 2
Reputation: 388982
You could extract the numbers from the column and create two new columns.
df[c('lat', 'lon')] <- stringr::str_extract_all(df$col, "-?\\d+", simplify = TRUE)
df
# col lat lon
#1 (33, -118) 33 -118
#2 (34, -119) 34 -119
#3 (36, -120) 36 -120
data
df <- data.frame(col = c('(33, -118)', '(34, -119)', '(36, -120)'))
Upvotes: 1
Reputation: 34703
In data.table
:
library(data.table)
setDT(myData)
myData[ , c('lat', 'lon') := tstrsplit(
gsub('[()]', '', lat_lon_col),
split = ', ',
fixed = TRUE, type.convert = TRUE
)]
Upvotes: 1