remove last char of a text string

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

remove last char of a text string

glaporta
Dear R experts,
is there a simple way to remove the last char of a text string?
substr() function use as parameter start end only... but my strings are of different length...
01asap05a -> 01asap05
02ee04b -> 02ee04
Thank you all,
Gianandrea
Reply | Threaded
Open this post in threaded view
|

Re: remove last char of a text string

Sarah Goslee
Sure. You can use nchar() to find out how long the string is.

> teststring <- "01asap05a"
> substr(teststring, 1, nchar(teststring)-1)
[1] "01asap05"

On Mon, Jun 14, 2010 at 9:47 AM, glaporta <[hidden email]> wrote:
>
> Dear R experts,
> is there a simple way to remove the last char of a text string?
> substr() function use as parameter start end only... but my strings are of
> different length...
> 01asap05a -> 01asap05
> 02ee04b -> 02ee04
> Thank you all,
> Gianandrea


--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: remove last char of a text string

Gustaf Rydevik
In reply to this post by glaporta
On Mon, Jun 14, 2010 at 3:47 PM, glaporta <[hidden email]> wrote:

>
> Dear R experts,
> is there a simple way to remove the last char of a text string?
> substr() function use as parameter start end only... but my strings are of
> different length...
> 01asap05a -> 01asap05
> 02ee04b -> 02ee04
> Thank you all,
> Gianandrea
> --
> View this message in context: http://r.789695.n4.nabble.com/remove-last-char-of-a-text-string-tp2254377p2254377.html
> Sent from the R help mailing list archive at Nabble.com.
>

It's not terribly elegant, but this works:

orig.text<-c("01asap05a","02ee04b")
substr(orig.text,1,nchar(orig.text)-1)

Regards,
Gustaf


--
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: remove last char of a text string

Henrique Dallazuanna
In reply to this post by glaporta
Try:

 gsub(".$", "", c('01asap05a', '02ee04b'))

On Mon, Jun 14, 2010 at 10:47 AM, glaporta <[hidden email]> wrote:

>
> Dear R experts,
> is there a simple way to remove the last char of a text string?
> substr() function use as parameter start end only... but my strings are of
> different length...
> 01asap05a -> 01asap05
> 02ee04b -> 02ee04
> Thank you all,
> Gianandrea
> --
> View this message in context:
> http://r.789695.n4.nabble.com/remove-last-char-of-a-text-string-tp2254377p2254377.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: remove last char of a text string

dobura
This post has NOT been accepted by the mailing list yet.
In reply to this post by Sarah Goslee
This is great!! thanks!!