Mutant
Mutant

Reputation: 3819

Filter data frame in r when string column value contains 'matching string'

I have data frame in following format

Question                Year
filter with question?   2010
keep this row           2009
keep this row too       2008
remove this one?        2007

expected results

Question                Year
keep this row           2009
keep this row too       2008

Get the subset of data frame excluding column Question contains question mark '?'.

Upvotes: 3

Views: 3884

Answers (1)

akrun
akrun

Reputation: 887851

We can use grep to filter out the ? elements in 'Question' column

df1[!grepl('\\?', df1$Question),]
#           Question Year
#2     keep this row 2009
#3 keep this row too 2008

Upvotes: 2

Related Questions