database - How to subset the rows of dataset from the values of another vector in R -
i have data frame this
n <- c("abc;xml", "abc;derm;sip", "xol;exp", "ban;lopic", "lpll2", "lpll") fac <- sample(n, 6, replace = f) d <- data.frame(x = 1:6, fac = fac) d x fac 1 1 abc;xml 2 2 ban;lopic 3 3 xol;exp 4 4 abc;derm;sip 5 5 lpll 6 6 lpll2
and vector this:
vec=c("abc", "xml","sip", "exp", "lopic", "lpll")
i subset rows have similar match values in vectors.
i tried code:
nam="abc|xml|sip|exp|lopic|lpll" subset(d, regexpr(nam, d$fac) > 0)
but doesn't work correctly, because include , lpll2!
the problem regex find match, if not exact. work:
index <- sapply(strsplit(as.character(d$fac), split = ";"), function(x) any(x%in% vec)) d[index, ] x fac 1 1 xol;exp 2 2 abc;xml 3 3 ban;lopic 5 5 lpll 6 6 abc;derm;sip
Comments
Post a Comment