我有一个数据框,我需要根据两个变量对其进行净化,但这两个变量在行中是“准相同的”。这意味着它们可以在一行中具有 -
或 '
或 s
或 :
或空格,但在另一排没有。
我确实使用了unique(),但此函数仅适用于相同的值。假设我们有这个 data.frame
Id<-c("RoLu1976","Rolu1976","AlBl1989","ThSa1996")
Art<-c("Econometric Policy Evaluation: A Critique","Econometric Policy Evaluations A Critique", "Rules after discretion", "Expectations and the Nonneutrality of Lucas")
Id.1<-c("FiKy1989","EdPr1986","BeBe1983","JoSt1989")
Art.1<-c("Notes on the Lucas Critique","Notes on the Lucas Critique","The Inconsistency of Optimal Plans","The Inconsistency of Optimal Plans")
N<-data.frame(Id,Art,Id.1,Art.1)
在第一个观察的变量 Art
中,准相同的值仅对于 s
和 :
不同。如何过滤和删除这些值?
最佳答案
根据您的数据,我使用 agrep
来匹配相似的字符串:
yy = NULL
for(i in 1:length(N$Art)){
temp = agrep(N[i,"Art"],N$Art,value=T)
y = ifelse(any(N[i,"Art"]==temp),temp[1],N[i,"Art"])
yy = c(yy,y)
}
然后将 N$Art
替换为 yy
,这将允许您使用重复/唯一
:
N$Art = yy
N.2 = N[!duplicated(N$Art), ]
关于R - 如何删除数据框中的两个准相同行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39802678/