Dear all, Is there a good way of doing the following conversion: [YYYY]-[MM]-[DD] [Time] [Day] [Name][Integer].[Extention] to become C:\test\[Name]\[YYYY]-[MM]-[DD] [Time] [Day]\[YYYY]-[MM]-[DD] [Time] [Day] [Name][Integer].[Extention] i.e. these 2009-04-10 1400 Fri Foo1.txt 2009-04-10 1400 Fri Universities2.txt 2009-04-10 1400 Fri Hitchhikers Guide To The Galaxy42.txt will become C:\test\Foo\2009-04-10 1400 Fri Foo1.txt C:\test\Universities\2009-04-10 1400 Fri Universities2.txt C:\test\Hitchhikers Guide To The Galaxy\2009-04-10 1400 Fri Hitchhikers Guide To The Galaxy42.txt My main issue is the conversion for 'Hitchkikers Guide To The Galaxy54' because of the spaces in the Name. So far this is what i have:> txt <- '2009-04-10 1400 Fri Universities1.txt' > step1 <- unlist(strsplit(txt, '\\.')) > step2 <- unlist(strsplit(step1[1], ' ')) > Name <- gsub('[0-9]',replacement='', step2[4]) > step3 <- paste(step2[1], step2[2], step2[3], sep=' ') > paste('C:\\test\\', Name, '\\', step3, '\\', txt, sep='' )[1] "C:\\test\\Universities\\2009-04-10 1400 Fri\\2009-04-10 1400 Fri Universities1.txt" Cheers, Tony Breyal
On 03-Jun-09 11:34:16, Tony Breyal wrote:> Dear all, > Is there a good way of doing the following conversion: > > [YYYY]-[MM]-[DD] [Time] [Day] [Name][Integer].[Extention] > > to become > > C:\test\[Name]\[YYYY]-[MM]-[DD] [Time] [Day]\[YYYY]-[MM]-[DD] [Time] > [Day] [Name][Integer].[Extention] > > i.e. these > > 2009-04-10 1400 Fri Foo1.txt > 2009-04-10 1400 Fri Universities2.txt > 2009-04-10 1400 Fri Hitchhikers Guide To The Galaxy42.txt > > will become > > C:\test\Foo\2009-04-10 1400 Fri Foo1.txt > C:\test\Universities\2009-04-10 1400 Fri Universities2.txt > C:\test\Hitchhikers Guide To The Galaxy\2009-04-10 1400 Fri > Hitchhikers Guide To The Galaxy42.txt > > My main issue is the conversion for 'Hitchkikers Guide To The > Galaxy54' because of the spaces in the Name. So far this is what i > have: > >> txt <- '2009-04-10 1400 Fri Universities1.txt' >> step1 <- unlist(strsplit(txt, '\\.')) >> step2 <- unlist(strsplit(step1[1], ' ')) >> Name <- gsub('[0-9]',replacement='', step2[4]) >> step3 <- paste(step2[1], step2[2], step2[3], sep=' ') >> paste('C:\\test\\', Name, '\\', step3, '\\', txt, sep='' ) > [1] "C:\\test\\Universities\\2009-04-10 1400 Fri\\2009-04-10 1400 Fri > Universities1.txt" > > Cheers, > Tony BreyalI can get as far as the following (using the "Hitchhikers" one): txt <- '2009-04-10 1400 Fri Hitchhikers Guide To The Galaxy42.txt' step1 <- unlist(strsplit(txt, '\\.')) step2 <- unlist(strsplit(step1[1], ' ')) step2 # [1] "Hitchhikers" "Guide" "To" "The" "Galaxy42" What is now needed is to join all the separate elements of step2 into a single character string. paste() won't do it, because it produces a separate character string for each element of step2. cat() won't do it because it has no value (so cannot be assigned). You could loop over step2: Name1 <- step2[4] for(i in (5:length(step2))) Name1 <- paste(Name1,step2[i]) Name1 # [1] "Hitchhikers Guide To The Galaxy42" Then do your gsub: Name <- gsub('[0-9]',replacement='', Name1) step3 <- paste(step2[1], step2[2], step2[3], sep=' ') paste('C:\\test\\', Name, '\\', step3, '\\', txt, sep='' ) [1] "C:\\test\\Hitchhikers Guide To The Galaxy\\2009-04-10 1400 Fri\\2009-04-10 1400 Fri Hitchhikers Guide To The Galaxy42.txt" So that works; but it would be nice to be able to avoid the loop! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 03-Jun-09 Time: 14:55:28 ------------------------------ XFMail ------------------------------
With txt <- c("2009-04-10 1400 Fri Foo1.txt", "2009-04-10 1400 Fri Universities2.txt", "2009-04-10 1400 Fri Hitchhikers Guide To The Galaxy42.txt") # Try this line:> sub("(.*) ([^0-9]+)([0-9]+).txt", "C:\\\\test\\\\\\2\\\\\\1 \\2\\3.txt", txt)[1] "C:\\test\\Foo\\2009-04-10 1400 Fri Foo1.txt" [2] "C:\\test\\Universities\\2009-04-10 1400 Fri Universities2.txt" [3] "C:\\test\\Galaxy\\2009-04-10 1400 Fri Hitchhikers Guide To The Galaxy42.txt" On Wed, Jun 3, 2009 at 7:34 AM, Tony Breyal <tony.breyal at googlemail.com> wrote:> Dear all, > > Is there a good way of doing the following conversion: > > [YYYY]-[MM]-[DD] [Time] [Day] [Name][Integer].[Extention] > > to become > > C:\test\[Name]\[YYYY]-[MM]-[DD] [Time] [Day]\[YYYY]-[MM]-[DD] [Time] > [Day] [Name][Integer].[Extention] > > i.e. these > > 2009-04-10 1400 Fri Foo1.txt > 2009-04-10 1400 Fri Universities2.txt > 2009-04-10 1400 Fri Hitchhikers Guide To The Galaxy42.txt > > will become > > C:\test\Foo\2009-04-10 1400 Fri Foo1.txt > C:\test\Universities\2009-04-10 1400 Fri Universities2.txt > C:\test\Hitchhikers Guide To The Galaxy\2009-04-10 1400 Fri > Hitchhikers Guide To The Galaxy42.txt > > My main issue is the conversion for 'Hitchkikers Guide To The > Galaxy54' because of the spaces in the Name. So far this is what i > have: > >> txt <- '2009-04-10 1400 Fri Universities1.txt' >> step1 <- unlist(strsplit(txt, '\\.')) >> step2 <- unlist(strsplit(step1[1], ' ')) >> Name <- gsub('[0-9]',replacement='', step2[4]) >> step3 <- paste(step2[1], step2[2], step2[3], sep=' ') >> paste('C:\\test\\', Name, '\\', step3, '\\', txt, sep='' ) > [1] "C:\\test\\Universities\\2009-04-10 1400 Fri\\2009-04-10 1400 Fri > Universities1.txt" > > Cheers, > Tony Breyal > > ______________________________________________ > 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. >
Reasonably Related Threads
- simplest way (set of functions) to parse a file
- couting events by subject with "black out" windows
- Reg an issue with smoothing factor in VAD implementation
- NMDS plot and Adonis (PerMANOVA) of community composition with presence absence and relative intensity
- Reg an issue with smoothing factor in VAD implementation