问题
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.
来源:https://stackoverflow.com/questions/62408768/is-there-a-way-to-replace-prime-in-a-string-using-str-replace-all