我有一个数据帧df1
,其中包含通过ID进行的获取信息。每个收单方A
和目标B
的四位SIC代码都用“ /”分隔。
df1 <- data.frame(ID = c(1,2,3,4),
A = c("1230/1344/2334/2334","3322/3344/3443", "1112/9099", "3332/4483"),
B = c("1333/2334","3344/8840", "4454", "9988/2221/4483"))
ID A B
1 1230/1344/2334/2334 1333/2334
2 3322/3344/3443 3344/8840
3 1112/9099 4454
4 3332/4483 9988/2221/4483
我将需要对每个交易ID进行如下分类:
所需的输出显示在更新的df1中。
df1 <- data.frame(ID = c(1,2,3,4),
A = c("1230/1344/2334/2334","3322/3344/3443", "1112/9099", "3332/4483"),
B = c("1333/2334","3344/8840", "4454", "9988/2221/4483"),
Primary.other.match = c(0,1,0,0), #only if primary Code of A or B matches
any other code of B or A
Other.other.match = c(1,0,0,1)) # only if primary codes do not match
primary or any other codes, but any other codes match
ID A B Primary.other.match Other.other.match
1 1230/1344/2334/2334 1333/2334 0 1
2 3322/3344/3443 3344/8840 1 0
3 1112/9099 4454 0 0
4 3332/4483 9988/2221/4483 0 1
谢谢您的帮助!
答案 0 :(得分:1)
这是tidyverse中的解决方案。
您首先创建一个函数,该函数检查是否存在主要匹配项或其他匹配项,然后使用c1 c2 prev
1 12 null
2 13 12
3 14 13
逐列应用此函数:
purrr::map