Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 比较两个字符串,但不考虑组成字符串的单词的位置_R_String - Fatal编程技术网

R 比较两个字符串,但不考虑组成字符串的单词的位置

R 比较两个字符串,但不考虑组成字符串的单词的位置,r,string,R,String,我知道,如果 s1 <- "string" s2 <- "String" 然而,我想到的是另一种比较: s1 <- "John Smith" s2 <- "Smith John" s1我们创建一个函数,将字符串按空格(strsplit)拆分,然后取消列表,排序,获取唯一的元素,将它们再次粘贴在一起 f1 <- function(x) toString(unique(sort(unli

我知道,如果

s1 <- "string"
s2 <- "String"
然而,我想到的是另一种比较:

s1 <- "John Smith"
s2 <- "Smith John"

s1我们创建一个函数,将字符串按空格(
strsplit
)拆分,然后
取消列表
排序
,获取
唯一的
元素,
将它们再次粘贴在一起

f1 <- function(x) toString(unique(sort(unlist(strsplit(tolower(x), " ")))))
f1(s1) == f1(s2)
#[1] TRUE

f1我们创建一个函数,将字符串按空格(
strsplit
)拆分,然后
取消列表
排序
,获取
唯一的
元素,
将它们再次粘贴在一起

f1 <- function(x) toString(unique(sort(unlist(strsplit(tolower(x), " ")))))
f1(s1) == f1(s2)
#[1] TRUE

f1这些字符串基本上是彼此的字谜。
您可以通过在map中存储这两个字符串并比较这两个map来检查它


dict(collection.Counter(S1)=dict(collection.Counter(S2)

这些字符串基本上是彼此的字谜。 您可以通过在map中存储这两个字符串并比较这两个map来检查它


dict(collection.Counter(S1)=dict(collection.Counter(S2)

使用
utf8ToInt

check_string <- function(s1, s2) {
     sum(utf8ToInt(tolower(s1))) == sum(utf8ToInt(tolower(s2)))
}

check_string("string", "string")
#[1] TRUE
check_string("string", "String")
#[1] TRUE
check_string("John Smith", "Smith John")
#[1] TRUE

使用
utf8ToInt
检查字符串:

check_string <- function(s1, s2) {
     sum(utf8ToInt(tolower(s1))) == sum(utf8ToInt(tolower(s2)))
}

check_string("string", "string")
#[1] TRUE
check_string("string", "String")
#[1] TRUE
check_string("John Smith", "Smith John")
#[1] TRUE
检查字符串