[R] how to Subset based on partial matching of columns?

samarvir singh samarvir1996 at gmail.com
Thu Apr 9 02:57:43 CEST 2015


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")`
'x <- c(1:5)
`df <- data.frame(x,code)`

df

     x 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
     x  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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list