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

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?

2

1 Answer

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))
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like