Re: [R] replace last 3 characters of string

From: Bert Jacobs <>
Date: Mon, 04 Apr 2011 01:48:00 +0200

I could imagine it was so simple.:)

-----Original Message-----
From: Dirk Eddelbuettel [] On Behalf Of Dirk Eddelbuettel
Sent: 04 April 2011 01:39
To: Bert Jacobs
Subject: Re: [R] replace last 3 characters of string

On Mon, Apr 04, 2011 at 01:39:34AM +0200, Bert Jacobs wrote:
> I would like to replace the last tree characters of the values of a
> column in a dataframe.
> This replacement should only take place if the last three characters
> correspond to the value "/:/" and they should be replaced with ""(blank)
> I cannot perform a simple gsub because the characters /:/ might also be
> present somewhere else in the string values and then they should not be
> replaced.

Keep reading up on regular expressions, this tends to pay off. Here we use the fact that you can achor a regexp to the end of a string:

R> aString <- "abc:def:::"
R> gsub(":::$", "", aString)
[1] "abc:def"

Hth, Dirk

Three out of two people have difficulties with fractions.

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Sun 03 Apr 2011 - 23:45:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Apr 2011 - 02:10:27 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive