简体   繁体   中英

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 (eg “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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM