Home > database >  Converting longitude entries from East and West to positive and negative
Converting longitude entries from East and West to positive and negative

Time:01-18

I currently have a data set listed as

Latitude     Longitude
lat:51.40    long:2.19W
lat:50.52    long:1.09E
lat:57.54    long:3.06W

How do I remove the W and E and make the W reading a negative in Rstudio?

after running dput(head(df)) as comment below suggested (the first few entries in the data set are errors so lat and long values were recorded as 0).

structure(list(deg_latitude = c(" deg_latitude: 0", " deg_latitude: 0", " deg_latitude: 0", " deg_latitude: 0", " deg_latitude: 0", " deg_latitude: 0"), deg_longitude = c(" deg_longitude: 0.0", " deg_longitude: 0.0", " deg_longitude: 0.0", " deg_longitude: 0.0", " deg_longitude: 0.0", " deg_longitude: 0.0")), row.names = c(NA, 6L), class = "data.frame")

CodePudding user response:

Remove all characters that is not digits and punctuation, and if "W" is detected (str_detect()) turn the value to negative

library(tidyverse) 

df %>%
  mutate(across(
    everything(),
    ~ case_when(
      str_detect(., "W") ~ as.numeric(str_remove_all(., "[^0-9\\.]")) * -1,
      TRUE ~ as.numeric(str_remove_all(., "[^0-9\\.]"))
    )
  ))

# A tibble: 3 × 2
  Latitude Longitude
     <dbl>     <dbl>
1     51.4     -2.19
2     50.5      1.09
3     57.5     -3.06

CodePudding user response:

data.table solution

library(data.table)
library(readr)
DT <- fread("Latitude     Longitude
lat:51.40    long:2.19W
lat:50.52    long:1.09E
lat:57.54    long:3.06W")

DT[, c("deg_lat", "deg_lon") := lapply(.SD, readr::parse_number)]
DT[grepl(".*W$", Longitude), deg_lon := -1 * deg_lon]

    Latitude  Longitude deg_lat deg_lon
1: lat:51.40 long:2.19W   51.40   -2.19
2: lat:50.52 long:1.09E   50.52    1.09
3: lat:57.54 long:3.06W   57.54   -3.06
  • Related