virsam
virsam

Reputation: 91

how to subset a data frame based on list of multiple match case in columns

So I have a list that contains certain characters as shown below

list <- c("MY","GM+" ,"TY","RS","LG")

And I have a variable named "CODE" in the data frame as follows

code <- c("MY GM+","","LGTY", "RS","TY")
df <- data.frame(1:5,code)
df

  code
1 MY GM+
2 
3 LGTY
4 RS
5 TY

Now I want to create 5 new variables named "MY","GM+","TY","RS","LG"

Which takes binary value, 1 if there's a match case in the CODE variable

df 
   code    MY GM+ TY RS LG
1  MY GM+  1  1   0  0  0
2          0  0   0  0  0
3  LGTY    0  0   1  0  1
4  RS      0  0   0  1  0 
5  TY      0  0   1  0  0

Really appreciate your help. Thank you.

Upvotes: 1

Views: 176

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

Since you know how many values will be returned (5), and what you want their types to be (integer), you could use vapply() with grepl(). We can turn the resulting logical matrix into integer values by using integer() in vapply()'s FUN.VALUE argument.

cbind(df, vapply(List, grepl, integer(nrow(df)), df$code, fixed = TRUE))
#     code MY GM+ TY RS LG
# 1 MY GM+  1   1  0  0  0
# 2         0   0  0  0  0
# 3   LGTY  0   0  1  0  1
# 4     RS  0   0  0  1  0
# 5     TY  0   0  1  0  0

I think your original data has a couple of typos, so here's what I used:

List <- c("MY", "GM+" , "TY", "RS", "LG")
df <- data.frame(code = c("MY GM+", "", "LGTY", "RS", "TY"))

Upvotes: 3

Related Questions