如何从R中的数据/转义特殊字符中扣除美元符号($)? [英] How do I strip dollar signs ($) from data/ escape special characters in R?

查看:327
本文介绍了如何从R中的数据/转义特殊字符中扣除美元符号($)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 gsub(toreplace,replacement,myvector)清除R中的数据。虽然这适用于逗号等, $没有任何效果。所以如果我做 gsub($,,myvector)所有的美元符号仍然存在。

I've been using gsub("toreplace","replacement", myvector) to clean out data in R. While this works for commas and the like, removing "$" has no effect. So if I do gsub("$","",myvector) all the dollar signs remain in place.

我认为这是因为 $ 是R中的一个特殊字符。我尝试转义它\ $产生相同的结果(无效果)。我在R中找不到一个转义特殊字符的资源。

I think this is because $ is a special character in R. I tried escaping it "\$" but that yields the same result (no effect). And I couldn't find a resource on escaping special characters in R.

显然,我应该在预处理中执行此操作。但是我想知道有没有人知道如何a)逃避R中的特殊字符b)直接在R中摆脱讨厌的 $ 。对于科学,

Obviously I should do this in preprocessing. But I was wondering if anyone out there knew how to either a) escape special characters in R b) get rid of pesky $ in R directly. For science.

推荐答案

你必须逃脱两次,首先是R,第二个是正则表达式。

You have to escape it twice, first for R, second for the regex.

gsub('\\$', '', c("a$a", "bb$"))
[1] "aa" "bb"

请参阅 ?引用有关引用和转义的详细信息。

See ?Quotes for details on quoting and escaping.

这篇关于如何从R中的数据/转义特殊字符中扣除美元符号($)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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