首页 > 解决方案 > 从数据框中删除在不同位置相同的单词

问题描述

在像这个库这样的 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","test1_test2",  "test2_test2_test2", "test2_other", "other"))

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

标签: rquanteda

解决方案


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

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" 

推荐阅读