r - 我想用空格替换双标点
问题描述
我写了以下代码,但它不起作用
replacePunctuation <- function(x) {
gsub("[[:punct:]]+" , " " , x)
}
sms_data_corpus_clean <- tm_map(sms_data_corpus_clean, replacePunctuation)
**I installed and loaded the library tm
注意:目的是避免使用以下示例中的标点符号:
Are you still.....there?
使用
sms_data_corpus_clean <- tm_map(sms_data_corpus_clean, removePunctuation)
结果,我们将这两个词连接起来
Are you stillthere
解决方案
我认为您可以在此处使用stringr
'str_replace
功能-
library(stringr)
replacePunctuation <- function(x){
# if there are multiple punctuations replace them
str_replace(x, pattern = "[[:punct:]]{2,}", " ")
}
sample_data <- c("2 or more punctuations - Are you still.....there?",
"only one punctuation - Are you still, here?")
replacePunctuation(sample_data)
#> [1] "2 or more punctuations - Are you still there?"
#> [2] "only one punctuation - Are you still, here?"
由 reprex 包(v0.2.0)于 2018 年 8 月 23 日创建。
推荐阅读
- mysql - MySQL:使用子查询返回 null 错误
- swift - 应用程序在将主上下文保存到子上下文的父级时不断崩溃
- php - 如何在 laravel 中的 C:\media 等外部目录中更改媒体上传/存储文件?
- django - 访问通过表单上的 OneToOneField 模型链接的模型的数据
- java - 使用 BufferedImage 加载图像时内存使用率高
- python - 将数据附加到 json 中的 Python 函数
- r - 无论来自 R 库 rWBclimate 的内容如何,我都会收到相同的错误
- r - 在 R markdown 中指定自定义 powerpoint 主题
- django - 我将如何摆脱错误“经理”对象没有属性“活动”
- dynamic - 使用 istio 根据流量的错误响应将流量分流到两个版本