基于组合集删除重复项
Remove Duplicates Based on Combined Sets
我有一个如下所示的数据框:
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 ))
数据框由一组两个位置的 latitude/longitude 坐标组成(A + B;即 A_Latitude
/A_Longitude
、B_Latitude
/B_Longitude
).
我想根据组合集删除重复项(即删除位置 A/Location B 等同于位置 B /位置 A 的行条目;即具有 A_Latitude
/ [= 的行12=] / B_Latitude
/ B_Longitude
= B_Latitude
/ B_Longitude
/ A_Latitude
/ A_Longitude
.
答案 and [Removing duplicate combinations (irrespective of order)] 没有帮助,因为这些解决方案没有考虑组合的列集(在考虑全球位置时,这些列在这里是相关的(例如,latitude/longitude 坐标等同于一个位置)).
预先感谢您的帮助。
一个想法是将每个 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"
我有一个如下所示的数据框:
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 ))
数据框由一组两个位置的 latitude/longitude 坐标组成(A + B;即 A_Latitude
/A_Longitude
、B_Latitude
/B_Longitude
).
我想根据组合集删除重复项(即删除位置 A/Location B 等同于位置 B /位置 A 的行条目;即具有 A_Latitude
/ [= 的行12=] / B_Latitude
/ B_Longitude
= B_Latitude
/ B_Longitude
/ A_Latitude
/ A_Longitude
.
答案
预先感谢您的帮助。
一个想法是将每个 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"