【发布时间】:2021-11-18 03:47:20
【问题描述】:
我有一个向量A,其中包含一个属列表,我想用它来子集第二个向量B。我已成功使用 grepl 从B 中提取与A 中的属部分匹配的任何内容。下面是我所做的一个可重现的示例。
但现在我想获得A 中的哪个属与B 中的某些内容匹配的列表,以及哪个属不匹配的列表。 IE。 “匹配”列表将包含 Cortinarius 和 Russula,而“不匹配”列表将包含 Laccaria 和 Inocybe。关于如何做到这一点的任何想法?实际上,我的向量很长,B 中的属名在其他信息中并不完全相同。
# create some dummy vectors
A <- c("Cortinarius","Laccaria","Inocybe","Russula")
B <- c("fafsdf_Cortinarius_sdfsdf","sdfsdf_Russula_sdfsdf_fdf","Tomentella_sdfsdf","sdfas_Sebacina","sdfsf_Clavulina_sdfdsf")
# extract the elements of B that have a partial match to anything in A.
new.B <- B[grepl(paste(A,collapse="|"), B)]
# But now how do I tell which elements of A were present in B, and which ones were not?
【问题讨论】: