简体   繁体   English

R:如何基于两列中的值获取丢失的记录

[英]R: How to get missing records based on values in two columns

I have two large dataframes of longitude/latitude coordinates, CoastalStates_Tax and CoastalStates , which are mostly the same except CoastalStates_Tax has a few million more coordinates. 我有两个大的经度/纬度坐标数据CoastalStates_TaxCoastalStates_TaxCoastalStates ,除了CoastalStates_Tax还有几百万个坐标外,它们几乎是相同的。 I want to figure out which rows in CoastalStates_Tax are not in CoastalStates , but still need to be able to track the indices of the missing rows wherever they are in the Tax dataset. 我想弄清楚CoastalStates_Tax中的哪些行不在CoastalStates ,但是无论在Tax数据集中的哪里,仍然需要能够跟踪丢失行的索引。

This is what CoastalStates_Tax looks like: 这就是CoastalStates_Tax样子:

  RecordID_b PROPERTY LEVEL LONGITUDE PROPERTY LEVEL LATITUDE
1  132381977                -77.06421                39.16937
2  132381978                -77.18106                39.08811
3  132381979                -77.03353                39.02414
4  132381980                -77.09930                39.00716
5  132381981                -77.25450                39.10422
6  132381982                -77.02797                39.08087

And CoastalStates : CoastalStates

  RecordID PROPERTY LEVEL LONGITUDE PROPERTY LEVEL LATITUDE
1        1                -80.24787                25.85063
2        2                -80.14940                25.84582
3        3                -80.13115                25.85699
4        4                -80.37275                25.77741
5        5                -80.12095                25.82633
6        6                -80.39949                25.73273

I tried using the dplyr anti_join function with anti_join(CoastalStates_Tax,CoastalStates,by=c("PROPERTY LEVEL LONGITUDE","PROPERTY LEVEL LATITUDE")) , but it only gives me 4,635,393 rows. 我尝试将dplyr anti_join函数与anti_join(CoastalStates_Tax,CoastalStates,by=c("PROPERTY LEVEL LONGITUDE","PROPERTY LEVEL LATITUDE")) ,但它只给我4,635,393行。
The difference in rows between the two datasets is 4,637,029, so I'm missing a about 1600 rows but I can't figure out why. 两个数据集之间的行差为4,637,029,所以我缺少大约1600行,但我不知道为什么。 Am I misusing anti_join , and if so, any suggestions on other ways to go about this? 我是否在滥用anti_join ,如果是,是否有其他建议可以解决此问题?

My suggestion will be to round both longitude and latitude to 3 decimal places (accuracy up to 110 meters) and than convert to character before joining on those columns. 我的建议是将经度和纬度都round到小数点后三位(精度最高为110米),而不是在加入这些列之前转换为character

An attempt can be as: 尝试可以是:

library(dplyr)

CoastalStates_Tax %>% 
mutate_at(vars(starts_with("PROPERTY.LEVEL")), funs(as.character(round(.,3)))) %>%
anti_join(mutate_at(CoastalStates, 
         vars(starts_with("PROPERTY.LEVEL")), funs(as.character(round(.,3)))),
            by=c("PROPERTY.LEVEL.LONGITUDE", "PROPERTY.LEVEL.LATITUDE"))

#   RecordID_b PROPERTY.LEVEL.LONGITUDE PROPERTY.LEVEL.LATITUDE
# 1  132381977                  -77.064                  39.169
# 2  132381978                  -77.181                  39.088
# 3  132381979                  -77.034                  39.024
# 4  132381980                  -77.099                  39.007
# 5  132381981                  -77.254                  39.104
# 6  132381982                  -77.028                  39.081

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM