如何使这些视觉上相同的字符串在计算上相等?

问题描述 投票:0回答:1

上下文:

我正在寻找基于字符向量的两个小标题,但是在write.csv()read.csv()之间发生了一些事情,使它们不相等。在下面的reprex中,str_cmp()返回0(“匹配”),但是在我的实际项目中,它返回了-1(字符串不可比)。我不知道为什么会改变。

在任何情况下,str_cmp()对我来说用处不大,因为dplyr::left_join联接成相等的值并且不能与函数一起使用。

我如何更改这些字符串之一,以便str1==str2返回TRUE?我需要能够对整个字符向量执行此操作,以便可以执行以下操作:

dplyr::left_join(tibble1,tibble2,by = c("charVector1"="charVector2")[charVector1charVector2是从中拉出str1str2的列。

提醒:

#DL 19/10/30
## Tryna work out why these strings aren't the same
#####################################################################

#Get strings from GitHub repo ---------------------------------------
read.table(
  "https://raw.githubusercontent.com/davelovellCARU/stringHelp/master/string1.txt"
) ->
  str1

read.table(
  "https://raw.githubusercontent.com/davelovellCARU/stringHelp/master/string2.txt"
) ->
  str2

# The strings are not equal -----------------------------------------
str1 == str2
#>       x
#> 1 FALSE
# But they look the same and the computer knows it ------------------
stringi::stri_cmp(str1, str2)
#> [1] 0

reprex package(v0.3.0)创建于2019-10-30

r string stringi
1个回答
0
投票

知道了!

有一个执行此操作的整洁函数:stringclean::replace_non_asci(string)我在两个字符串上都运行了,现在它们是相同的。只需将其卡入一个变异体中,小滴就会加入。

© www.soinside.com 2019 - 2024. All rights reserved.