【问题标题】:R - How delete two quasi-identical rows of a data frame?R - 如何删除数据框的两个准相同的行?
【发布时间】:2016-10-01 03:18:11
【问题描述】:

我有一个数据框,我需要根据两个变量对其进行净化,但两个变量在行中都是“准相同的”。这意味着他们可以在一行中有一个-'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: 不同。如何过滤和删除这些值?

【问题讨论】:

    标签: r dataframe delete-row


    【解决方案1】:

    根据您的数据,我使用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), ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-06
      • 2022-06-28
      • 1970-01-01
      • 2020-09-28
      • 2020-09-16
      • 2019-04-07
      • 2017-02-15
      • 1970-01-01
      相关资源
      最近更新 更多