Killian
Killian

Reputation: 89

How to remove characters in r

I have a character sting like "x$var1". I want to eliminate the "x$" so I only have "var1". It is probably simple but I am new to R. Any help would be appreciated. Thanks in advance

Upvotes: 1

Views: 124

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70623

You can use function sub. Double \\ is used because $ is a special regular expression character, so it needs to be escaped.

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

Or we can use fixed=TRUE and remove the escape characters\\

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

Upvotes: 3

Related Questions