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
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
In data.table
:
library(data.table)
setDT(myData)
myData[ , c('lat', 'lon') := tstrsplit(
gsub('[()]', '', lat_lon_col),
split = ', ',
fixed = TRUE, type.convert = TRUE
)]
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)'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.