1

I have a set of dataset, but I would like to extract rows that contain specific keywords.

I tried using the codes below, and it works. But I just wondering if there is any better way in doing this.

test1 <- subset(data, grepl("love|hate", Content))
test2 <- subset(data, grepl("love|hate", Articles))
together <- unique(rbind(test1, test2))

I tried combining together, but failed to do so:

test1 <- subset(data, grepl("love|hate", Content, Articles))
  • provide some sample datas.. – Avinash Raj Nov 02 '15 at 06:06
  • 2
    You probably need to combine first `Content` and `Articles`, if they are vectors of strings (possibly removing duplicates). –  Nov 02 '15 at 06:09
  • 2
    Try `unique(do.call(rbind, lapply(data[c('Content', 'Articles')], function(x) subset(x, grepl('love|hate', x))))j)` – akrun Nov 02 '15 at 06:12
  • 3
    It's really not clear what you're trying to achieve here - is it just `subset(data, grepl("love|hate", Content) & grepl("love|hate", Articles) )` ? – thelatemail Nov 02 '15 at 06:14
  • Take a look [here](http://stackoverflow.com/q/5963269/903061) for advice on making good, reproducible questions. – Gregor Thomas Nov 02 '15 at 06:23

1 Answers1

1

Try this:

data[grepl("love|hate",data$Content) & grepl("love|hate",data$Articles),]
Carl
  • 5,569
  • 6
  • 39
  • 74