2015-12-14 15 views
5

処理できるようにするには、:の最初のオカレンスを文字列(これは私のマーカーであり、スピーチが始まる)に置き換えます。":"の最初のオカレンスをRに置き換えます。

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 

を使用して第1 :の位置を見つけることができていますしかし、私は(と言う、例えば、|で)textでそれを置き換えることができません。

答えて

13

subは、パターン:の最初のオカレンスと一致するので、それを|に置き換えて使用できます。

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

@Thomas 'サブ'は ':'の最初の出現を置き換えます。私はそれがあなたが質問で尋ねたものだと思います。 – akrun

+0

これは\t恥ずかしいです。ありがとうございました! – Thomas

+0

これは 'sub()'と 'gsub()'の違いです。 – RHertel

2

またstringrパッケージからstr_replaceを使用することができます。

text1 <- c("ABC:DEF:", "SDF", "::ASW") 
library(stringr) 
str_replace(text1, ":", "|") 
# [1] "ABC|DEF:" "SDF"  "|:ASW" 

これは|:の最初の発生を置き換えます。

関連する問題