匹配混乱的字符串并返回R

时间:2017-12-29 05:54:20

标签: r dplyr subset mutate

下面是一个数据框,在R中有两列字符数据类型。我的要求是假设我有一个混乱的字符串" FB,AI,IT,GE",我想测试一下混乱的字符串存在于" a2列"中,然后返回相应的" a1列"值。显然,第一个" a2列" value是与上面混乱的字符串匹配的,我需要相应的" a1"要退回的价值。我很感激使用" if-else"避免。谢谢,请帮助。

a1 = c("B1","B2","B3","B4","B5")
a2 = c("IT,GE,FB,AI","GE,AI","FB,IT,AI","GE,IT,FB","AI")
a12 = data.frame(a1,a2)
a12$a1 = as.character(a12$a1)
a12$a2 = as.character(a12$a2)

1 个答案:

答案 0 :(得分:0)

希望这有帮助!

#string to be matched
match_str <- "FB,AI,IT,GE"

idx <- sapply(strsplit(as.character(a12$a2),","), 
              function(x) setequal(unlist(strsplit(as.character(match_str),",")), unlist(x)))
a12$a1[idx]

输出是:

[1] "B1"

#sample data
> dput(a12)
structure(list(a1 = c("B1", "B2", "B3", "B4", "B5"), a2 = c("IT,GE,FB,AI", 
"GE,AI", "FB,IT,AI", "GE,IT,FB", "AI")), .Names = c("a1", "a2"
), row.names = c(NA, -5L), class = "data.frame")