有没有办法使用str_replace_all替换字符串中的′(素数)?

时间:2020-06-16 12:41:49

标签: r replace unicode utf-8 stringr

在尝试通过Measurements :: conv_unit()之前,我尝试以度/分钟和度/分钟/秒为单位设置各种坐标格式,这需要输入以空格分隔的数字。我已经阅读了类似问题的各种答案,例如:Remove all special characters from a string in R?

这使我最初尝试:

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

但是,没有删除撇号(')。在参数中调用符号或其编码是否存在问题?

我已经探索了替代符号的其他方式:

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

但是这些选项都不能提供我所需的组合:保留一些字符(-和。),而删除其他字符。我确实更喜欢pattern = c(“°|'| \” |'|“”)提供的控件,因为我正在建立具有自动数据整理功能的数据库,因此我可以指定坐标中通常包含的符号。

我是否错过了使用str_replace_all()的简单解决方案?失败表明字符串编码有问题吗?

当前正在运行R版本4.0.1和tidyverse_1.3.0。

0 个答案:

没有答案
相关问题