I have vectors x
and type
x <- c("value", "class", "value2", "value3", "class2", "other")
type <- c("value", "class")
and like to match x
to type
based on type patterns.
E.g. with grepl("^value", x)
I get a logical vector of matching patterns for the element "value" in type
. Using some function of the apply
family helps to generalize, but how to match this to x
?
Hope what I'd like to achieve is comprehensible, my desired result in this small example is
c("value", "class", "value", "value", "class")
One Note: I prefer a base r solution over tidyverse!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…