How to check in every row in a column if it contains a substring

user3569522 picture user3569522 · Apr 24, 2014 · Viewed 8.9k times · Source

Let's say I have a column 'name' in the dataframe df:

apple
apple123
app
be
aple

and, I want to check if every row in the name column contains the word apple. The way I did it was to use grepl, grepl('apple',df$name), I was hoping it would return 'TRUE','TRUE','FALSE','FALSE','FALSE', however, it returned 5 'FALSE' instead.

Did I do anything wrong here, if not grepl, what function should I use?

Answer

Paulo E. Cardoso picture Paulo E. Cardoso · Apr 24, 2014

I get it running fine

dat <- c('apple', 'apple123', 'app', 'be', 'aple')
grepl('apple', dat)
[1]  TRUE  TRUE FALSE FALSE FALSE
dat[grepl('apple', dat)]
[1] "apple"    "apple123"

This is exactly the same with a data.frame

dat <- data.frame(v=c('apple', 'apple123', 'app', 'be', 'aple'))
grepl('apple', dat$v)
[1]  TRUE  TRUE FALSE FALSE FALSE

which is the same if you do

with(dat, grepl('apple', v))