简体   繁体   English

如何删除r中的字符

[英]How to remove characters in r

I have a character sting like "x$var1" . 我有一个像"x$var1"的字符。 I want to eliminate the "x$" so I only have "var1" . 我想消除"x$"所以我只有"var1" It is probably simple but I am new to R. Any help would be appreciated. 它可能很简单,但我是R的新手。任何帮助都将不胜感激。 Thanks in advance 提前致谢

You can use function sub . 你可以使用功能sub Double \\\\ is used because $ is a special regular expression character, so it needs to be escaped. 使用Double \\\\是因为$是一个特殊的正则表达式字符,因此需要进行转义。

sub("x\\$", replacement = "", x = "x$var1")
[1] "var1"

Or we can use fixed=TRUE and remove the escape characters \\\\ 或者我们可以使用fixed=TRUE并删除转义字符\\\\

sub("x$", replacement = "", x = "x$var1", fixed=TRUE)
#[1] "var1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM