Is there a way to replace ′ (prime) in a string using str_replace_all?

心已入冬 提交于 2020-06-17 09:10:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!