【问题标题】:Remove Duplicates Based on Combined Sets基于组合集删除重复项
【发布时间】:2018-05-16 17:13:27
【问题描述】:

我有一个如下所示的数据框:

C <- data.frame(A_Latitude  = c(48.4459, 48.7     , 49.0275, 49.0275,   49.0275, 49.0275,   48.4459),
            A_Longitude = c(9.989    , 8.15   , 8.7539 , 8.7539 ,   8.7539 , 8.7539 , 9.989  ),
            B_Latitude  = c(49.0275, 48.4734,   48.4459, 48.9602,   48.9602, 48.4459,   49.0275),
            B_Longitude = c(8.7539 , 9.227  ,   9.989    , 9.2058 , 9.2058 , 9.989  , 8.7539 ))

数据框由一组两个位置(A + B;即A_Latitude/A_LongitudeB_Latitude/B_Longitude)的纬度/经度坐标组成。

我想根据组合集删除重复项(即,删除位置 A/位置 B 等于位置 B /位置 A 的行条目;即具有 A_Latitude / A_Longitude / B_Latitude / 的行B_Longitude = B_Latitude / B_Longitude / A_Latitude / A_Longitude.

[Finding unique combinations irrespective of position [duplicate]][Removing duplicate combinations (irrespective of order)] 的答案没有帮助,因为这些解决方案没有考虑组合的列集(在考虑全球位置时,这与此处相关(例如,纬度/经度坐标对于一个位置是等效的) )。

提前感谢您的帮助。

【问题讨论】:

    标签: r dataframe geolocation


    【解决方案1】:

    一个想法是将每个 long/lat 对视为一个字符串 toString(...) - 对每行的两个 long/lat 对(现在是字符串)进行排序 - 然后对生成的 2 元素字符串向量进行排序。使用已排序的字符串向量检查重复项

    ans <- C[!duplicated(lapply(1:nrow(C), function(i) sort(c(toString(C[i,1:2]), toString(C[i,3:4]))))), ]
      # A_Latitude A_Longitude B_Latitude B_Longitude
    # 1    48.4459      9.9890    49.0275      8.7539
    # 2    48.7000      8.1500    48.4734      9.2270
    # 4    49.0275      8.7539    48.9602      9.2058
    

    这是第 1 行的细分

    toString(C[1,1:2])
    # [1] "48.4459, 9.989"
    toString(C[1,3:4])
    # [1] "49.0275, 8.7539"
    sort(c(toString(C[1,1:2]), toString(C[1,3:4])))
    # [1] "48.4459, 9.989"  "49.0275, 8.7539"
    

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 2015-02-20
      • 2015-11-24
      相关资源
      最近更新 更多