Harry Smith
Harry Smith

Reputation: 278

%in% when vector has unstructured elements

I want to subset a dataframe like

ID <- paste(1:10, LETTERS[1:10], sep = "")
var1 <- c(paste(rep("Gene", 4), 1:4, sep = ""), "Gene15 /// Gene11", 
paste(rep("Gene", 5), 6:10, sep = ""))
df <- data.frame("ID" = ID, "var1" = var1)
tomatch <- c(paste(rep("Gene", 10), 2:11, sep = ""))

want <- df[df$var1 %in% 
tomatch, ]

But df$var1 is a vector that contains some elements with single values (e.g. Gene1), and some elements with multiple elements with a separator " /// " (e.g. Gene15 /// Gene11). tomatch is a vector of names: c(Gene1, Gene2, Gene3...). Thank you.

Upvotes: 1

Views: 43

Answers (1)

CLedbetter
CLedbetter

Reputation: 96

want <- df[sapply(strsplit(df$var1, ' /// '), 
                  function(x) any(x %in% tomatch)), ]

want

    ID              var1
2   2B             Gene2
3   3C             Gene3
4   4D             Gene4
5   5E Gene15 /// Gene11
6   6F             Gene6
7   7G             Gene7
8   8H             Gene8
9   9I             Gene9
10 10J            Gene10

Upvotes: 1

Related Questions