Hi all, I am looking for a function in R to trim the last two characters of an 8 character string in a vector. For example, I have the codes 37-079-2, 370079-3,37-079-8 and want to trim them to 37-079 by removing the last two characters. Is sub the correct function to use, and if so how can I specify trimming the last 2 characters? I have read the help file, but can't quite figure out how to do it. Thanks, Wade
See ?substr and ?nchar
Try:
substr("Hello World", 0, nchar("Hello World")-2)
Regards
Markus Gesmann
FPMA
Lloyd's Market Analysis
Lloyd's * One Lime Street * London * EC3M 7HA
Telephone +44 (0)20 7327 6472
Facsimile +44 (0)20 7327 5718
http://www.lloyds.com
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Wade Wall
Sent: 24 July 2006 14:42
To: r-help at stat.math.ethz.ch
Subject: [R] trim function in R
Hi all,
I am looking for a function in R to trim the last two characters of an
8 character string in a vector. For example, I have the codes
37-079-2, 370079-3,37-079-8 and want to trim them to 37-079 by
removing the last two characters. Is sub the correct function to use,
and if so how can I specify trimming the last 2 characters? I have
read the help file, but can't quite figure out how to do it.
Thanks,
Wade
______________________________________________
R-help at stat.math.ethz.ch 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.
**********************************************************************
The information in this E-Mail and in any attachments is CON...{{dropped}}
One option is substr()> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Wade Wall > Sent: Monday, July 24, 2006 9:42 AM > To: r-help at stat.math.ethz.ch > Subject: [R] trim function in R > > Hi all, > I am looking for a function in R to trim the last two characters of an > 8 character string in a vector. For example, I have the > codes 37-079-2, 370079-3,37-079-8 and want to trim them to > 37-079 by removing the last two characters. Is sub the > correct function to use, and if so how can I specify trimming > the last 2 characters? I have read the help file, but can't > quite figure out how to do it. > Thanks, > Wade > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
A . (dot) matches any character and $ matches the end of string so
this replaces the last two characters with the empty string:
sub("..$", "", x)
On 7/24/06, Wade Wall <wade.wall at gmail.com>
wrote:> Hi all,
> I am looking for a function in R to trim the last two characters of an
> 8 character string in a vector. For example, I have the codes
> 37-079-2, 370079-3,37-079-8 and want to trim them to 37-079 by
> removing the last two characters. Is sub the correct function to use,
> and if so how can I specify trimming the last 2 characters? I have
> read the help file, but can't quite figure out how to do it.
> Thanks,
> Wade
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>