0

So, im trying to separate the values from this column of a datased that has Latitude and Longitude together. This is how the values are separated

df <- data.frame(localidade = c(drugs_location))
df %>% separate(localidade, c("Lat", "Long"), sep = ",")

I used this code above to separate the values in Lat and Long but now i need to use those values to make a plot using the Lat as the x axis and Long as the y axis.

This is the dput(head(df)) output as requested:

    structure(list(localidade = c("(42.30676881, -71.08431925)", 
"(42.30676881, -71.08431925)", "(42.33152148, -71.07085307)", 
"(42.31580934, -71.07618683)", "(42.31580934, -71.07618683)", 
"(42.31224532, -71.07317861)")), row.names = c(NA, 6L), class = "data.frame")
  • 2
    Please do not post pictures. Welcome to SO! Please take a moment to read about how to post R questions: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – YOLO Sep 28 '20 at 15:25
  • 1
    Hi, can you upload your dataframe so that we can more effectively help? Try `dput(df)` and then copy and paste the entire output. If your dataframe is large then try `dput(head(df))`. – TheSciGuy Sep 28 '20 at 15:26
  • 1
    Im pretty new to r so i hope the output is correct – Guilherme Frediani Sep 28 '20 at 15:30
  • Nearly a duplicate of https://stackoverflow.com/q/54617973/5325862 – camille Sep 28 '20 at 19:21

1 Answers1

1
library(stringr) 
library(dplyr)
df %>%
  mutate(localidade = str_replace_all(localidade, "\\(|\\)", "")) %>%
  separate(localidade, c("Lat", "Long"), sep = ",") %>%
  mutate(Lat = as.numeric(Lat), Long = as.numeric(Long))

You need to convert the values to numeric. The first mutate deletes all the "(" or ")" from the string.
Then you seperate the string with the "," and convert to numeric.

Wawv
  • 371
  • 2
  • 6