James
James

Reputation: 1457

R data.table conditional find/replace

I have a data.table (sbd_sbmolbio_n). I need to find rows where 2 conditions are true:

ORF_SEQUENCE contains “MKTIIALSYIFCLVFA"

N_TAG contains “Signal Seq”

Then I need to replace the “Signal Seq” part of the N_TAG with “HA”, but leave the rest of the string as-is (e.g. “Signal Seq-10XHis-Tev” becomes “HA-10XHis-Tev”

I am trying this:

sbd_sbmolbio_n[grep("MKTIIALSYIFCLVFA",ORF_SEQUENCE),][grep("Signal Seq", N_TAG), N_TAG := sub("Signal Seq", "HA", N_TAG)]

It is finding the rows, but the substitution is not being made. Any thoughts?

Upvotes: 0

Views: 242

Answers (1)

eddi
eddi

Reputation: 49448

The first set of brackets returns a data.table that's not the original data.table anymore, and then you're modifying that one. To do this in place - combine both conditions (notice the use of grepl instead of grep):

sbd_sbmolbio_n[grepl("MKTIIALSYIFCLVFA",ORF_SEQUENCE) & grepl("Signal Seq", N_TAG),
               N_TAG := sub("Signal Seq", "HA", N_TAG)]

Upvotes: 1

Related Questions