我有20多个不同的数据表,这些数据表由要加入一个表的同一编码系统(北美工业分类系统,NAICS)索引。
问题是每个表中都有不同级别的详细信息,当我加入时,我想通过使编码系统的层次结构失败直到找到匹配项来找到最佳匹配项。
常规left_join
无效,因为不会总是存在完全匹配项。我看过fuzzyjoin
程序包,但是有点麻烦。
我想从一个代码表开始
t_master
# A tibble: 360 x 1
NAICS17
<chr>
1 311111
2 311119
3 311211
4 311212
5 311213
6 311221
7 311224
8 311225
9 311230
10 311313
# ... with 350 more rows
t_master <- structure(list(NAICS17 = c(311111L, 311119L, 311211L, 311212L,
311213L, 311221L, 311224L, 311225L, 311230L, 311313L)), row.names = c(NA,
-10L), class = "data.frame")
,然后循环浏览所有其他表,以找到每个表中最佳的匹配项,然后将所有变量联接在一起。有时这很简单,因为存在完全匹配的内容(t_asm中的311111和311119将与t_master中的311111和311119结合在一起):
t_asm
# A tibble: 8,167 x 3
NAICS17 CEXBLD CEXMCH
<chr> <chr> <chr>
1 31-33 16806796 96986337
2 311 2099542 9063451
3 3111 92429 517196
4 31111 92429 517196
5 311111 49756 225494
6 311119 42673 291702
7 3112 192911 1016770
8 31121 75310 267693
9 31121M 75310 267693
10 31122 94339 546407
# ... with 8,157 more rows
t_asm <- structure(list(NAICS17 = c("31-33", "311", "3111", "31111", "311111",
"311119", "3112", "31121", "31121M", "31122"), CEXBLD = c("16806796",
"2099542", "92429", "92429", "49756", "42673", "192911", "75310",
"75310", "94339"), CEXMCH = c("96986337", "9063451", "517196",
"517196", "225494", "291702", "1016770", "267693", "267693",
"546407")), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
但是有时候我希望它寻找最佳匹配,即使细节不太详细(来自t_brdis_2015的311将同时加入到t_master中的311111和311119)
t_brdis_2015
# A tibble: 90 x 3
NAICS17 rdcost_total rdcost_wage
<chr> <chr> <chr>
1 0 355821 204170
2 31 236132 129375
3 32 236132 129375
4 33 236132 129375
5 311 4838 2945
6 312 1002 532
7 313 748 481
8 314 748 481
9 315 748 481
10 316 748 481
# ... with 80 more rows
t_brdis_2015 <- structure(list(NAICS17 = c("0", "31", "32", "33", "311", "312",
"313", "314", "315", "316"), rdcost_total = c("355821", "236132",
"236132", "236132", "4838", "1002", "748", "748", "748", "748"
), rdcost_wage = c("204170", "129375", "129375", "129375", "2945",
"532", "481", "481", "481", "481")), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -10L))
这将是左连接,我希望将t_master的所有观察结果与其他数据表中的一个观察结果相结合。
故障回复的方法是(以311111为例):
谢谢,让我知道是否有任何不清楚的地方。
答案 0 :(得分:1)
我将执行一系列更新连接:
library(data.table)
ncs = seq_len(max(nchar(t_master$NAICS17)))
nms = copy(names(t_asm))
xnms = sprintf("x.%s", nms)
tnms = replace(nms, nms == "NAICS17", "m")
t_asm2 <- data.table(t_asm)
out = data.table(t_master)
out[, NAICS17 := as.character(NAICS17)]
out[, m := NA_character_]
for (nc in rev(ncs)){
out[is.na(m), target := substr(NAICS17, 1, nc)]
out[is.na(m),
(tnms) := t_asm2[.SD, on=.(NAICS17 = target), mget(xnms)][]
]
if (!anyNA(out$m)) break
}
out[, target := NULL][]
NAICS17 m CEXBLD CEXMCH
1: 311111 311111 49756 225494
2: 311119 311119 42673 291702
3: 311211 31121 75310 267693
4: 311212 31121 75310 267693
5: 311213 31121 75310 267693
6: 311221 31122 94339 546407
7: 311224 31122 94339 546407
8: 311225 31122 94339 546407
9: 311230 3112 192911 1016770
10: 311313 311 2099542 9063451
m
是匹配的值; target
是我们在循环的当前迭代中尝试匹配的值。迭代从最长的代码开始向后进行。 (查看正在迭代的rev(ncs)
。)
通过过滤到is.na(m)
,我们将跳过在早期迭代中已匹配的行。测试anyNA(out$m)
使我们能够在每行都匹配的情况下提前退出。
copy
只是避免Why does data.table update names(DT) by reference, even if I assign to another variable?