Kim Milferstedt
2007-Sep-17 15:21 UTC
[R] removing a specific number of digist from a character string
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
Paul Hiemstra
2007-Sep-17 15:58 UTC
[R] removing a specific number of digist from a character string
Hi Kim, You could try: substr("060907_17_3_5_1_1_2909.tif", start = 1, stop = 18) ?substr cheers, Paul Kim Milferstedt schreef:> 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. >-- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +31302535773 Fax: +31302531145 http://intamap.geo.uu.nl/~paul
Jeffrey Robert Spies
2007-Sep-17 15:59 UTC
[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.