在R中首次出现特定字符之前删除所有文本 [英] Remove all text before first occurence of specific characeter in R
本文介绍了在R中首次出现特定字符之前删除所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看下面的向量:
x <- c("MED - This means medic - somecode123", "HIV" - This means HIV -somecode456")
现在我想要向量:包含值
Now I want the vector: containing the values
This means medic - somecode123`
This means HIV - somecode1456
我似乎无法使用gsub
...
I seem not able to solve this using gsub
...
推荐答案
我们可以使用sub
.匹配一个或多个非空白(\\S+
),然后是一个或多个空白(\\s+
),然后是-
和空白(\\s+
)的模式,并将其替换为""
.
We can use sub
. Match the pattern of one or more non-white space (\\S+
) followed by one or more white space (\\s+
) followed by -
and white space (\\s+
) and replace it with ""
.
sub('\\S+\\s+-\\s+', "", x)
#[1] "This means medic - somecode123" "This means HIV -somecode456"
这篇关于在R中首次出现特定字符之前删除所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文