r - 从数据框中删除在不同位置相同的单词
问题描述
在像这个库这样的 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"))
我找到了这个解决方案示例,但它删除了相同的单词
解决方案
在下划线处拆分字符串并按字母顺序排序,然后使用此列表识别重复项并将其应用于原始列表:
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"
推荐阅读
- sql - 在嵌套选择查询中按条件分组后加入
- mysql - 查找用户输入的值或对行值求和
- javascript - 如何使用 django-channels 2 处理消息?
- python - 我有以下结构的列表,需要从中返回数据
- heroku - 在 Heroku 上配置域不起作用
- aframe - 自定义标记不显示任何内容
- python - TypeError:“分页”对象在 Flask 中不可迭代
- angular - Angular:当可以下载n个项目时如何设置下载微调器
- linux - libpcap是否使用了linux的流量控制层?
- python-3.x - 无法使用 paramiko sudo su