【问题标题】:Replace first occurrence from the end of a string in R替换R中字符串末尾的第一次出现
【发布时间】:2016-04-05 10:06:37
【问题描述】:

作为一个例子,我有这个向量

c("qw;erty;qwert;qwe;", "ty;qwert;qw")

如何使用sub 函数或任何其他函数来替换第一次出现的“;”从带有“\t”的行尾开始,所以结果将是c("qw;erty;qwert;qwe\t", "ty;qwert\tqw")

【问题讨论】:

  • 试试sub(";([^;]*)$","\t\\1",c("qw;erty;qwert;qwe;", "ty;qwert;qw"))
  • @nicola 我没有看到你的评论,但它惊人地相似

标签: r string character


【解决方案1】:

我们可以试试

sub(";([^;]*)$", "\t\\1", str1)
#[1] "qw;erty;qwert;qwe\t" "ty;qwert\tqw"  

数据

str1 <- c("qw;erty;qwert;qwe;", "ty;qwert;qw")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 2011-06-05
    • 2011-08-25
    • 2016-11-02
    相关资源
    最近更新 更多