[Solved] Create vectors containing 0 or 1 based on subjects in R [closed]


t(  apply(mydf[-1], 1, function(x)  as.numeric ( c(   
   # need the t() to change columns to rows
 any( grepl("POLLUTION|EMISSION|WASTE", x) ),
 any(grepl("OIL\\sSPILL", x) ), 
 any(grepl("MERGER|ACQUI", x) ), 
 any(grepl("MERGER|ACQUI", x) )      )
       )     )
 )
 #-------
  [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    1    0    1    1
[3,]    1    1    0    0
[4,]    1    1    1    1

 cbind(mydf, .Last.value)
     ARTICLE              SUBJECT.1               SUBJECT.2
1 I'M ARTICLE #1              POLLUTION                   FRAUD
2 I'M ARTICLE #2               ACQUIRED     POLLUTION & DAMAGES
3 I'M ARTICLE #3        INSIDER TRADING FRAUD & INSIDER TRADING
4 I'M ARTICLE #4 MERGERS & ACQUISITIONS              OIL SPILLS
           SUBJECT.3 1 2 3 4
1                OIL 1 0 0 0
2           BIOFUELS 1 0 1 1
3 OIL SPILLS & WASTE 1 1 0 0
4          EMISSIONS 1 1 1 1

There are probably more elegant methods, but this seemed sufficiently “obvious” that this bear of little brain could put it down on “paper”. The naming of the columns seems sufficiently trivial that it can be left as “an exercise for the reader”.

solved Create vectors containing 0 or 1 based on subjects in R [closed]