简体   繁体   English

用Rpply匹配R中的两个字符向量

[英]matching two character vectors in R with lapply

I want to match items from two character vectors, "A" and "B", two find out two things: 1) whether items from vector A appear in vector B (yes/no) and 2) which items from vector B do not appear in vector A? 我想匹配来自两个字符向量的项目“A”和“B”,两个找出两件事:1)向量A中的项是否出现在向量B中(是/否)和2)来自向量B的项不是出现在向量A中?

The two vectors look like this: 这两个向量看起来像这样:

A <- c("i", "u", "I", "U", "E", "V", "@", "{", "$", "#", "Q", "1", "2", "3", "4", "5", "6", "7", "8", "9")
B <- c("1", "1", "1", "1", "#", "$", "$", "1", "2", "2", "1", "d", "d", "i", "i", "i", "i", "1", "3", "2", "2", "F", "2", "2", "2", "5", "5", "5", "@", "5", "6", "5", "z", "z", "S", "S")

I can partially answer my first question with this function: 我可以用这个函数部分回答我的第一个问题:

test_match <- function(item_vector_A, item_vector_B){
ifelse(item_vector_A == item_vector_B, print(1), print(0))
}

lapply(A, B, FUN = test_match) -> results

However, when I try this, I get a list of each comparison the function has made: 但是,当我尝试这个时,我得到了该函数所做的每个比较的列表:

lapply(A, B, FUN = test_match) -> results
results
[[1]]
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[[2]]
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[[3]]
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#etc.

How can I get just a simple list that indicates for each item in A whether it occurs in B (1) or not (0), like this: 我怎样才能得到一个简单的列表,表明A中的每个项目是否出现在B(1)或不出现(0),如下所示:

1 0 0 0 0 0 1 0 1 1 0 1 1 1 0 1 1 0 0 0 

I have the same problem when I try to answer my second question: 当我尝试回答第二个问题时,我遇到了同样的问题:

test_non_match <- function(item_vector_A, item_vector_B){
ifelse(item_vector_B == item_vector_A, print("*match*"), print(item_vector_B))
}
lapply(A, B, FUN = test_non_match) -> results2
results2
[[1]]
[1] "1" "1" "1" "1" "#"  "$" "$" "1" "2" "2" "1" "d" "d" "*match*" "*match*" "*match*" "*match*" "1" "3" "2" "2" "F" "2" "2" "2" "5" "5" "5" "@" "5" "6" "5" "z" "z" "S" "S"      
[[2]]
[1] "1" "1" "1" "1" "#" "$" "$" "1" "2" "2" "1" "d" "d" "i" "i" "i" "i" "1" "3" "2" "2" "F" "2" "2" "2" "5" "5" "5" "@" "5" "6" "5" "z" "z" "S" "S"
[[3]]
[1] "1" "1" "1" "1" "#" "$" "$" "1" "2" "2" "1" "d" "d" "i" "i" "i" "i" "1" "3" "2" "2" "F" "2" "2" "2" "5" "5" "5" "@" "5" "6" "5" "z" "z" "S" "S"

It lists the whole vector, whereas I would like to have something like this: 它列出了整个矢量,而我希望有这样的东西:

[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] d
[1] d
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] F
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] *match*
[1] z
[1] z
[1] S
[1] S

Do I need to use another type of apply() function? 我是否需要使用其他类型的apply()函数?

除了上面的替代方案,您可能需要查看%chin%,这是data.table包中%in%的更快版本:

ifelse (B %chin% A, "*match*", B)

You could just use %in% and test for A %in% B and !(B %in% A ) . 您可以只使用%in%并测试A %in% B!(B %in% A ) To reproduce the output in your question: 要在您的问题中重现输出:

as.numeric(A %in% B)
 [1] 1 0 0 0 0 0 1 0 1 1 0 1 1 1 0 1 1 0 0 0

and as suggested by Ferdinand.kraft: 并按照Ferdinand.kraft的建议:

ifelse (B %in% A, "*match*", B)
 [1] "*match*" "*match*" "*match*" "*match*" "*match*" "*match*" "*match*" "*match*" "*match*" "*match*" "*match*" "d"       "d"       "*match*" "*match*" "*match*" "*match*" "*match*" "*match*"
[20] "*match*" "*match*" "F"       "*match*" "*match*" "*match*" "*match*" "*match*"  "*match*" "*match*" "*match*" "*match*" "*match*" "z"       "z"       "S"       "S"      

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

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