替换R中的子字符串并与数值冲突

时间:2019-03-18 20:52:48

标签: r gsub

我想用" (log2)"代替"ETS2 (log2)",但是我使用的命令也是用“ 2”代替了“ ETS2”。

创建字符串

t = as.character("ETS2 (log2)")
t2 = as.character("ETS1 (log2)")

这是我尝试的代码。

    #for t1
    gsub("[ (log2)]", "", t)
    str_replace_all(t, "[[ (log2)]]", "")
    #for t2
    gsub("[ (log2)]", "", t2)
    str_replace_all(t2, "[[ (log2)]]", "")

输出:

    #for t1
    "ETS "
    "ETS "
    #for t2
    "ETS1 "
    "ETS1 "

所需的输出:

    #for t1
    "ETS2 "
    "ETS2 "
    #for t2
    "ETS1 "
    "ETS1 "

0 个答案:

没有答案