0

Using this command it keeps the rows which have the specific word

df[df$ID == "interesting", ]

If this word is exist in the row but it has more words around how is it possible to find if this word exist and keep the row.

Example input

data.frame(text = c("interesting", " I am interesting for this", "remove")

Expected output

data.frame(text = c("interesting", " I am interesting for this")
Nathalie
  • 1,228
  • 7
  • 20

1 Answers1

2

1.Example data:

df <- data.frame(text = c("interesting", " I am interesting for this", "remove"),
                 stringsAsFactors = FALSE)

Solution using base R. Indexing using grepl:

df[grepl("interesting", df$text), ]

This returns:

[1] "interesting"                " I am interesting for this"

Edit 1

Change code so that it returns a data.frame and not a vector.

df[grep("interesting", df$text), , drop = FALSE]

This now returns:

                        text
1                interesting
2  I am interesting for this
Community
  • 1
  • 1
dario
  • 6,415
  • 2
  • 12
  • 26