Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

grepl

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?

like image 668
user3569522 Avatar asked Apr 24 '14 15:04

user3569522


1 Answers

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))
like image 126
Paulo E. Cardoso Avatar answered Oct 28 '22 12:10

Paulo E. Cardoso