0

I'm trying to format various coordinates in degrees/minutes and degrees/minutes/seconds prior to passing through measurements::conv_unit(), which requires the input as numbers separated by spaces. I've read various answers to similar questions, such as this one: Remove all special characters from a string in R?

Which lead me to initially try:

library(tidyverse)
latitude <- "-36°48′31.33"
str_replace_all(string = latitude, pattern = c("°|'|\"|′|″"), repl = " ") 

However, the prime symbol (′) is not being removed. Is this an issue with calling the symbol in the argument, or its encoding?

I have explored other ways of substituting symbols:

str_replace_all(string = temp_core$description$latitude, pattern = "[^[:alnum:]]", repl=" ") #removes all symbols including . and -
str_replace_all(string = temp_core$description$latitude, pattern = "[[:punct:]]", repl=" ") #removes most symbols including . and - but excluding °
iconv(temp_core$description$latitude, "utf-8", "ascii", sub = " ") # removes the unwanted symbols but replaces them with an uneven number of spaces

But none of these options give me the combination I need: retaining some characters (- and .) while dropping the others. I do prefer the control that pattern = c("°|'|\"|′|″") provides as I am building a database with automated data wrangling and therefore I can specify symbols commonly included in coordinates.

Is there a simple solution I have missed that uses str_replace_all()? Is the failure indicative of an issue with the encoding of the string?

Currently running R version 4.0.1 and tidyverse_1.3.0.

jvan257
  • 73
  • 5
  • the prime symbol is removed when i run the code – maarvd Jun 16 '20 at 13:32
  • @maarvd Okay, that tells me that there is something different on my end in terms of encoding. Do you have any other advice? – jvan257 Jun 16 '20 at 19:31
  • 2
    @maarvd see: https://stackoverflow.com/questions/62419815/why-is-r-coercing-to-when-i-try-to-assign-it-as-a-simple-character-vector-bu – André.B Jun 17 '20 at 01:14

0 Answers0