简体   繁体   English

R:“模糊匹配”和“之间”语句

[英]R: "Fuzzy Match" and "Between" Statements

I am working with the R Programming Language.我正在使用 R 编程语言。 I have the following tables (note: all variables appear as "Factors"):我有以下表格(注意:所有变量都显示为“因素”):

table_1 = data.frame(id = c("123", "123", "125", "C125-B"), 
date_1 = c("2010-01-31","2010-01-31", "2016-01-31", "2018-01-31" ))

table_1$id = as.factor(table_1$id)
table_1$date_1 = as.factor(table_1$date_1)

table_2 = data.frame(id = c("5123", "123 A", "125", "125"), 
date_2 = c("2009-01-31","2010-01-31", "2010-01-31", "2010-01-31" ),
date_3 = c("2011-01-31","2010-01-31", "2020-01-31", "2020-01-31" ))


table_2$id = as.factor(table_2$id)
table_2$date_2 = as.factor(table_2$date_2)
table_2$date_3 = as.factor(table_2$date_3)


> table_1
      id     date_1
1    123 2010-01-31
2    123 2010-01-31
3    125 2016-01-31
4 C125-B 2018-01-31

 table_2
     id     date_2     date_3
1  5123 2009-01-31 2011-01-31
2 123 A 2010-01-31 2010-01-31
3   125 2010-01-31 2020-01-31
4   125 2010-01-31 2020-01-31

I am trying to "join" (eg inner join) this tables on the following conditions:我正在尝试在以下条件下“加入”(例如内部联接)此表:

1) if table_1$id "fuzzy equal" table_2$id 1) if table_1$id "fuzzy equal" table_2$id

AND

2) if table_1$date BETWEEN(table_2$date_2,table_2$date_3) 2) if table_1$date BETWEEN(table_2$date_2,table_2$date_3)

I tried to write the following code in R to do this:我尝试在 R 中编写以下代码来执行此操作:

library(fuzzyjoin)
stringdist_inner_join(table_1, table_2,
                      by ="id", distance_col = NULL)

Question: But I am not sure if the stringdist_inner_join function can accommodate this kind of "between" logic.问题:但我不确定stringdist_inner_join function 是否可以容纳这种“介于”逻辑。

Can someone please show me how to do this?有人可以告诉我如何做到这一点吗? Are there any other methods to accomplish this in R?在 R 中是否还有其他方法可以实现此目的?

Thanks!谢谢!

How about this?这个怎么样? We could do the stringdist_inner_join and filter afterwards if the dates are stored as dates.如果日期存储为日期,我们可以执行 stringdist_inner_join 并在之后进行过滤。 This should be plenty performant for most data, and if not you should probably use data.table instead of fuzzyjoin.对于大多数数据,这应该足够高效,如果不是,您可能应该使用 data.table 而不是模糊连接。

library(fuzzyjoin)
library(dplyr)
table_1$date_1 = as.Date(table_1$date_1)
table_2$date_2 = as.Date(table_2$date_2)
table_2$date_3 = as.Date(table_2$date_3)
stringdist_inner_join(table_1, table_2, by = "id", max_dist = 2) %>%
  filter(date_1 >= date_2, date_1 <= date_3)


   id.x     date_1  id.y     date_2     date_3
1   123 2010-01-31  5123 2009-01-31 2011-01-31
2   123 2010-01-31 123 A 2010-01-31 2010-01-31
3   123 2010-01-31   125 2010-01-31 2020-01-31
4   123 2010-01-31   125 2010-01-31 2020-01-31
5   123 2010-01-31  5123 2009-01-31 2011-01-31
6   123 2010-01-31 123 A 2010-01-31 2010-01-31
7   123 2010-01-31   125 2010-01-31 2020-01-31
8   123 2010-01-31   125 2010-01-31 2020-01-31
9   125 2016-01-31   125 2010-01-31 2020-01-31
10  125 2016-01-31   125 2010-01-31 2020-01-31

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

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