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 certain > 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" R> Hth, Dirk -- Three out of two people have difficulties with fractions.
Hi, I would like to replace the last tree characters of the values of a certain 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. Could someone help me out on this one? Thx Bert [[alternative HTML version deleted]]
Thx I could imagine it was so simple.:) Bert -----Original Message----- From: Dirk Eddelbuettel [mailto:edd at master.debian.org] On Behalf Of Dirk Eddelbuettel Sent: 04 April 2011 01:39 To: Bert Jacobs Cc: r-help at r-project.org 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 acertain> 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" R> Hth, Dirk -- Three out of two people have difficulties with fractions.
Will this do it for you:> x <- c('asdfasdf', 'sadfasdf/:/', 'sadf', 'asdf/:/') > sub("/:/$", '', x)[1] "asdfasdf" "sadfasdf" "sadf" "asdf" On Sun, Apr 3, 2011 at 7:39 PM, Bert Jacobs <bert.jacobs at figurestofacts.be> wrote:> Hi, > > > > I would like to replace the last tree characters of the values of a certain > 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. > > > > Could someone help me out on this one? > > Thx > > Bert > > > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Bert Jacobs-2 wrote:> > I would like to replace the last tree characters of the values of a > certain > column in a dataframe. > >Besides the mentioned standard method: I found the subset of string operations in Hadley Wickhams stringr package helpful. They have a much more consistent interface compared to the standard interface. str_sub: accepts negative positions, which are calculated from the left of the last character. Dieter -- View this message in context: http://r.789695.n4.nabble.com/replace-last-3-characters-of-string-tp3424354p3425855.html Sent from the R help mailing list archive at Nabble.com.