R - 如何删除数据框的两个准相同的行?
R - How delete two quasi-identical rows of a data frame?
我有一个数据框,我需要根据两个变量对其进行净化,但这两个变量都在 "quasi-identical" 行中。这意味着他们可以在一行中有 -
或 '
或 s
或 :
或 space 但在另一行中没有。
我确实使用了 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
,这将允许您使用 duplicated/unique
:
N$Art = yy
N.2 = N[!duplicated(N$Art), ]
我有一个数据框,我需要根据两个变量对其进行净化,但这两个变量都在 "quasi-identical" 行中。这意味着他们可以在一行中有 -
或 '
或 s
或 :
或 space 但在另一行中没有。
我确实使用了 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
,这将允许您使用 duplicated/unique
:
N$Art = yy
N.2 = N[!duplicated(N$Art), ]