Arthur Pennt
Arthur Pennt

Reputation: 155

Detecting a word in a string in R

I have the BigQuery Dataset with Reddit Comments. It has multiple columns, one which is the body column with the actual comment. I now want to search for a certain word, like a brand mention, for instance "BMW" in the body column and create a subset of the rows which contain "BMW" in data$body.

The dataset looks similar to this:

str(data)
data.frame: 75519 obs. of 113 variables
$ body: chr "...." .....
$ name: Factor w/ 22805 levels ....
....

I know the SQL command, which looks like this

SELECT * FROM dataset
WHERE body contains "BMW"

Is there a similar command in R?

Thank you very much!

EDIT: Solutions is

 bmw <- data[grep("BMW", data$body),]

Thanks to charleslmh

Upvotes: 1

Views: 311

Answers (2)

IRTFM
IRTFM

Reputation: 263301

Either of these would succeed:

bmw <- data[ grep("BMW", data$body), ]  # numerical indexing
bmw <- data[ grepl("BMW", data$body), ] # logical indexing

The second one will succeed because the "[" function selects rows where logical vectors are TRUE in the "i" (the first) position.

Upvotes: 1

Arthur Pennt
Arthur Pennt

Reputation: 155

The solution is

bmw <- data[grep("BMW", data$body),]

Thanks to charleslmh

Upvotes: 2

Related Questions