[英]R data.table conditional find/replace
I have a data.table (sbd_sbmolbio_n). 我有一个data.table(sbd_sbmolbio_n)。 I need to find rows where 2 conditions are true: 我需要找到满足2个条件的行:
ORF_SEQUENCE contains “MKTIIALSYIFCLVFA" ORF_SEQUENCE包含“ MKTIIALSYIFCLVFA”
N_TAG contains “Signal Seq” N_TAG包含“信号序列”
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” 然后,我需要用“ HA”替换N_TAG的“ Signal Seq”部分,但其余字符串保持原样(例如,“ Signal Seq-10XHis-Tev”变为“ 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. 第一组括号返回的data.table
不再是原始的data.table
,然后您将其修改。 To do this in place - combine both conditions (notice the use of grepl
instead of grep
): 为此,请结合两个条件(注意使用grepl
代替grep
):
sbd_sbmolbio_n[grepl("MKTIIALSYIFCLVFA",ORF_SEQUENCE) & grepl("Signal Seq", N_TAG),
N_TAG := sub("Signal Seq", "HA", N_TAG)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.