当前位置:  开发笔记 > 编程语言 > 正文

在R中替换第一次出现的":"而不是第二次

如何解决《在R中替换第一次出现的":"而不是第二次》经验,为你挑选了1个好方法。

为了能够处理我想要替换:字符串中的第一个匹配项(这是我的标记,语音开始).

text <- c("Mr. Mark Francois (Rayleigh) (Con): If the scheme was so poorly targeted, why were the Government about to roll it out to employees in the Department of Trade and Industry and the Department for Work and Pensions on the very day the Treasury scrapped it? The CBI and the TUC have endorsed the scheme, which has helped 500,000 people and their families to improve their computer skills. When the Chancellor announced the original concession, he told the  Daily Record:", "Even at this eleventh hour, will the Government recognise that this is a poor decision, taken by an analogue Chancellor who is stuck in the past and reversing?", "Dawn Primarolo: The hon. Gentleman answers his own question, as the US does not have similar schemes. He is right to address the question of how we give people in the greatest need access to computer technology, but the Low Pay Commission\u0092s 2005 report showed that that was not happening under the scheme. Why should the Government spend £200 million on a poorly targeted scheme? It was being abused and was not delivering, so the Government have refocused to ensure that the objective is achieved.")

我能够找到第一次:使用的位置

lapply(gregexpr("\\:", text), head, 1)

[[1]]
[1] 35

[[2]]
[1] -1

[[3]]
[1] 15

但是,我无法替换它text(例如,用a |).



1> akrun..:

我们可以使用,sub因为它只匹配模式的第一次出现,:然后我们用它替换|.

sub(':', '|', text)


@Thomas"sub"只替换了`:`的第一次出现.我想这就是你在问题中提出的问题.
推荐阅读
殉情放开那只小兔子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有