[R] removing a specific number of digist from a character string

Bert Gunter gunter.berton at gene.com
Mon Sep 17 19:10:50 CEST 2007


Please note that the second expression below should be:

 sub('[[:digit:]]{4}\\.tif', '', test)

The backslashes must be doubled: (from the ?regexpr Help file:

"Patterns are described here as they would be printed by cat: do remember
that backslashes need to be doubled when entering R character strings from
the keyboard. ")

Bert Gunter
Genentech Nonclinical Statistics


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Jeffrey Robert Spies
Sent: Monday, September 17, 2007 8:59 AM
To: Kim Milferstedt
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] removing a specific number of digist from a character
string

test <- c("060907_17_3_5_1_1_2909.tif", "060907_17_3_5_2_1_2910.tif",  
"060907_17_3_5_3_1_2911.tif")
sub('[[:digit:]][[:digit:]][[:digit:]][[:digit:]]\.tif', '', test)

or

test <- c("060907_17_3_5_1_1_2909.tif", "060907_17_3_5_2_1_2910.tif",  
"060907_17_3_5_3_1_2911.tif")
sub('[[:digit:]]{4}\.tif', '', test)

-- Jeff.

On Sep 17, 2007, at 11:21 AM, Kim Milferstedt wrote:

> Hello,
>
> I would like to remove the last 8 digists of character strings in a
> vector. Below I added a couple of elements from that vector.
>
> I have a problem defining a pattern to replace the digits using for
> example "sub". Removing the ".tif" part works fine using
> sub('.tif',"",x), but how do I get rid of the four preceding digits?
>
> Thanks for your help,
>
> Kim
>
> Input:
> [3988]
> "060907_17_3_5_1_1_2909.tif"   "060907_17_3_5_2_1_2910.tif"
> "060907_17_3_5_3_1_2911.tif"
> [3991]
> "060907_17_3_5_4_1_2912.tif"   "060907_17_3_5_5_1_2913.tif"
> "060907_17_3_5_6_1_2914.tif"
>
> Desired output:
> [3988] "060907_17_3_5_1_1_"   "060907_17_3_5_2_1_"    
> "060907_17_3_5_3_1_"
> [3991] "060907_17_3_5_4_1_"   "060907_17_3_5_5_1_"    
> "060907_17_3_5_6_1_"
>
> __________________________________________
>
> Kim Milferstedt
> University of Illinois at Urbana-Champaign
> Department of Civil and Environmental Engineering
> 4125 Newmark Civil Engineering Laboratory
> 205 North Mathews Avenue MC-250
> Urbana, IL 61801
> USA
> phone: (001) 217 333-9663
> fax: (001) 217 333-6968
> email: milferst at uiuc.edu
> http://cee.uiuc.edu/research/morgenroth
>
> ______________________________________________
> 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.

______________________________________________
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.



More information about the R-help mailing list