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

Jeffrey Robert Spies jspies at nd.edu
Mon Sep 17 17:59:15 CEST 2007


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.



More information about the R-help mailing list