R - 查找顺序相反的单词的重复项

R - Finding duplicates of words that are in a reversed order

我有一个 data.table,其中有一列包含职业名称。我想找出重复的职业,但以相反的顺序书写(例如作家广告和广告作家)。 这是我的数据和我想要得到的结果的简化版本

data = data.table(
  ID = as.character(c("advertisings writer","writer advertisings","setter","drill setter","setter drill","agent claims","claims agent","engineer"))
)
data_result = data.table(
  ID = as.character(c("advertisings writer","setter","drill setter","agent claims","engineer"))
)

这是我一直在使用的代码。

data[,b:= strsplit(ID," ")]

data <- data[,.(b=unlist(b)),by = setdiff(names(data),'b')]
setorderv(data,cols=c("ID","b"))
data <- data[,bb:=list(list(unique(b))),by="ID"][,.SD[1],by=c("ID"),.SDcols=c("bb")]
data[,b:=lapply(bb,paste,collapse=' ')]
data[,b:=unlist(b)]

unique(data,by="b")

因为我要处理相当大的数据集,所以这种方法非常耗时。

谢谢

data.table的可能解决方案:

  1. 将字符串拆分为单词
  2. 对单词进行排序
  3. 粘贴排序的单词
  4. 获取唯一值
library(data.table)

data[,ID:=sapply(sapply(stringr::str_split(ID,' '),sort),function(x) paste(x,collapse=' '))]
unique(data)

                    ID
1: advertisings writer
2:              setter
3:        drill setter
4:        agent claims
5:            engineer

这里有一个igraph选项

library(dplyr)
library(igraph)

data[, TO := gsub("(\w+)\s(\w+)", "\2 \1", ID)] %>%
  graph_from_data_frame(directed = FALSE) %>%
  get.data.frame() %>%
  unique() %>%
  subset(select = from)

这给出了

                 from
1 advertisings writer
3              setter
4        drill setter
6        agent claims
8            engineer