大佬教程收集整理的这篇文章主要介绍了如何删除一组特定字符之前的所有内容(例如,“?-”)? 数据,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想删除“? -”之前的所有内容,包括字符本身(问号、空格、连字符、空格)。我知道可以删除特定字符之前的所有内容。我不想在连字符 - 之前删除所有内容,因为我还有其他带有连字符的语句。我试过了,但它不适用于带连字符的单词。
示例:
gsub(".*-","","To what extent do you disagree or agree with the following statements? - Statistics make me cry.")
gsub(".*-","To what extent do you disagree or agree with the following statements? - T-tests are easy to interpret.")
Output:
" Statistics make me cry."
"tests are easy to interpret."
我希望第二个语句显示为 T-tests are easy to interpret
这里 sub
代替全局 g
就足够了。更改模式以匹配 ?
(元字符 - 因此它被转义 \\
),后跟零个或多个空格(\\s*
),后跟 -
,然后零或更多空格,替换为空白 (''
)
sub(".*\\?\\s*-\\s*","",v1)
#[1] "Statistics make me cry." "T-tests are easy to interpret."
v1 <- c("To what extent do you disagree or agree with the following statements? - Statistics make me cry.","To what extent do you disagree or agree with the following statements? - T-tests are easy to interpret."
)
,
您可以尝试包 Stringr
并使用 str_split
。但是它将结果放在一个列表中,因此您必须从第一个列表中提取第二个元素。或者开始你输入一个向量。
library(Stringr)
str1 <- "To what extent do you disagree or agree with the following statements? - Statistics make me cry."
str2 <- "To what extent do you disagree or agree with the following statements? - T-tests are easy to interpret."
str_split(str1,pattern = "`? - ")[[1]][2]
[1]“统计数据让我哭了。”
str_split(str2,pattern = "`? - ")[[1]][2]
[1]“T 检验很容易解释。”
,如果您想在 Stringr
中使用 tidyverse
:
library(Stringr)
str <- c("To what extent do you disagree or agree with the following statements? - Statistics make me cry.","To what extent do you disagree or agree with the following statements? - T-tests are easy to interpret.")
str_split(str,' - ',simplify = T)[,2]
以上是大佬教程为你收集整理的如何删除一组特定字符之前的所有内容(例如,“?-”)? 数据全部内容,希望文章能够帮你解决如何删除一组特定字符之前的所有内容(例如,“?-”)? 数据所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。