如何删除R中的特定字符 [英] How to remove specific special characters in R

查看:4671
本文介绍了如何删除R中的特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的句子。

  c =在酸碱反应为什么它会创造水而不是H +? 

我要删除除'?& + - /

$以外的所有特殊字符b
$ b

我知道如果我想删除所有特殊字符,我可以使用

  gsub([[:punct:]],,c)
在酸性反应page4中为什么会产生水而不是H

但是,一些特殊字符如+ - ?也被删除,我打算保留。



我尝试创建一个特殊字符串,我可以在这样的代码中使用

  gsub([special_string],,c)

最好我能做的是想出这个

  cat(!\#$% ; 



p> gsub([cat(!\#$%),但是下面的代码不起作用



< ()* ,.:; = = @ [\\] ^ _`{|}〜。)],,c)
pre>

除了一些我想保留的特殊字符之外,我应该怎么做才能删除?



感谢

解决方案

  gsub([^ [:alnum:] [:blank: +?&/ / \\-],,c)
#[1]在酸碱反应page4中为什么会产生水而不是H +?


I have some sentences like this one.

c = "In Acid-base reaction (page[4]), why does it create water and not H+?" 

I want to remove all special characters except for '?&+-/

I know that if I want to remove all special characters, I can simply use

gsub("[[:punct:]]", "", c)
"In Acidbase reaction page4 why does it create water and not H"

However, some special characters such as + - ? are also removed, which I intend to keep.

I tried to create a string of special characters that I can use in some code like this

gsub("[special_string]", "", c)

The best I can do is to come up with this

cat("!\"#$%()*,.:;<=>@[\\]^_`{|}~.")

However, the following code just won't work

gsub("[cat("!\"#$%()*,.:;<=>@[\\]^_`{|}~.")]", "", c)

What should I do to remove special characters, except for a few that I want to keep?

Thanks

解决方案

gsub("[^[:alnum:][:blank:]+?&/\\-]", "", c)
# [1] "In Acid-base reaction page4 why does it create water and not H+?"

这篇关于如何删除R中的特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆