我有一个 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")
由于我正在处理相当大的数据集,这种方法非常耗时。
谢谢