2014-02-15 115 views
0

我有每個級別都是單詞的字符向量。它是從一段文字中生成的,其中一些段用尖括號標出。這些部分的長度不同。我需要將標記的段合併到矢量中。合併字符向量中的特定特定字符串

輸入如下:

c("This","is","some","text","with","<marked","up","chunks>[L]","in","it") 

我需要的輸出看起來像這樣:

c("This","is","some","text","with","<marked up chunks>[L]","in","it") 

感謝。

+2

可以有「嵌套」標記段?例如c(「a」,「」,「b>」)?如果是這樣,你需要做什麼? – digEmAll

+0

一個矢量中可以有多個塊嗎? –

+0

沒有嵌套標記,但一個矢量中有多個塊。 – Andreas

回答

0

這裏的,也可用於多塊的向量的方法:

vec <- c("This","is","some","text","with","<marked","up","chunks>[L]","in","it") 

from <- grep("<", vec) 
to <- grep(">", vec) 

idx <- mapply(seq, from, to, SIMPLIFY = FALSE) 

new_strings <- sapply(idx, function(x) 
    paste(vec[x], collapse = " ")) 

replacement <- unlist(mapply(function(x, y) c(y, rep(NA, length(x) - 1)), 
          idx, new_strings, SIMPLIFY = FALSE)) 

new_vec <- "attributes<-"(na.omit(replace(vec, unlist(idx), replacement)), NULL) 


[1] "This"     "is"     
[3] "some"     "text"     
[5] "with"     "<marked up chunks>[L]" 
[7] "in"     "it"