0

I have a column which is like below in my data table:

Category
_________
{A}
{B}
{C}
{A,B}
{A,C}
{C}
{C,A,B}
{A}
{A,C}

from this I want to select where the rows where A is a member of category. I am currently using

DT[Category=="{A}"]

to filter out the rows where category= A but rather than this I would like to have rows which category contains A (like member of in postgres) . Any help is appreciated.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Ricky
  • 2,662
  • 5
  • 25
  • 57

2 Answers2

2
# Data
df <- read.delim(textConnection('Category
{A}
{B}
{C}
{A,B}
{A,C}
{C}
{C,A,B}
{A}
{A,C}'))


# Slice, keeping only rows in 'Category' containing character 'A'
subset(df, grepl('A', Category))

  Category
1      {A}
4    {A,B}
5    {A,C}
7  {C,A,B}
8      {A}
9    {A,C}
tagoma
  • 3,896
  • 4
  • 38
  • 57
0

grep should come handy here

DT[grep('A',DT$Category,ignore.case = FALSE),]
Gangesh Dubey
  • 382
  • 1
  • 7