从数据框中删除不同位置相同的单词

问题描述

在这样的 dfm 中包含单词 图书馆(“quanteda”)

软件包版本:2.1.2

dfmat <- dfm(c("hello_text","text_hello","test1_test2","test2_test1","test2_test2_test2","test2_other","other"))

例如标记“hello_text”和“text_hello”在不同的地方是相同的。怎么可能只保留其中一个选项?

示例输出

dfmat <- dfm(c("hello_text","other"))

我找到了 this 解决方案示例,但它删除了相同的单词

解决方法

在下划线处拆分字符串并按字母顺序排序,然后使用此列表识别重复项并将其应用于原始列表:

words <- c("hello_text","text_hello","test1_test2","test2_test1","test2_test2_test2","test2_other","other")

words_sorted <- sapply(sapply(words,strsplit,"_"),sort)

words[!duplicated(words_sorted)]

返回:

[1] "hello_text"        "test1_test2"       "test2_test2_test2" "test2_other"      
[5] "other" 

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...