[R] Extracting File Basename without Extension

2009-01-08 Thread Gundala Viswanath
Dear all, The basename() function returns the extension also: > myfile <- "path1/path2/myoutput.txt" > basename(myfile) [1] "myoutput.txt" Is there any other function where it just returns plain base: "myoutput" i.e. without 'txt' - Gundala Viswanath Jakarta - Indonesia

Re: [R] Extracting File Basename without Extension

2009-01-08 Thread jim holtman
You can use 'sub' to get rid of the extensions: > sub("^([^.]*).*", "\\1", 'filename.extension') [1] "filename" > sub("^([^.]*).*", "\\1", 'filename.extension.and.more') [1] "filename" > sub("^([^.]*).*", "\\1", 'filename without extension') [1] "filename without extension" On Thu, Jan 8, 2009 a

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Henrique Dallazuanna
Try this also: substr(basename(myfile), 1, nchar(basename(myfile)) - 4) On Fri, Jan 9, 2009 at 12:10 AM, Gundala Viswanath wrote: > Dear all, > > The basename() function returns the extension also: > > > myfile <- "path1/path2/myoutput.txt" > > basename(myfile) > [1] "myoutput.txt" > > > Is ther

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Berwin A Turlach
G'day all, On Fri, 9 Jan 2009 08:12:18 -0200 "Henrique Dallazuanna" wrote: > Try this also: > > substr(basename(myfile), 1, nchar(basename(myfile)) - 4) Or, in case that the extension has more than three letters or "myfile" is a vector of names: R> myfile <- "path1/path2/myoutput.txt" R> sapp

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Wacek Kusnierczyk
Berwin A Turlach wrote: > G'day all, > > On Fri, 9 Jan 2009 08:12:18 -0200 > "Henrique Dallazuanna" wrote: > > >> Try this also: >> >> substr(basename(myfile), 1, nchar(basename(myfile)) - 4) >> > > Or, in case that the extension has more than three letters or "myfile" > is a vector of nam

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Gabor Grothendieck
On Fri, Jan 9, 2009 at 6:52 AM, Wacek Kusnierczyk wrote: > Berwin A Turlach wrote: >> G'day all, >> >> On Fri, 9 Jan 2009 08:12:18 -0200 >> "Henrique Dallazuanna" wrote: >> >> >>> Try this also: >>> >>> substr(basename(myfile), 1, nchar(basename(myfile)) - 4) >>> >> >> Or, in case that the extens

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Wacek Kusnierczyk
Gabor Grothendieck wrote: > On Fri, Jan 9, 2009 at 6:52 AM, Wacek Kusnierczyk > > >> or have sub do the job for you: >> >> filenames.ext = c("foo.bar", basename("foo/bar/hello.dolly")) >> (filenames.noext = sub("[.][^.]*$", "", filenames.ext, perl=TRUE)) >> > > We can omit perl = TRUE here.

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Berwin A Turlach
G'day Wacek, On Fri, 09 Jan 2009 12:52:46 +0100 Wacek Kusnierczyk wrote: > Berwin A Turlach wrote: > > G'day all, > > > > On Fri, 9 Jan 2009 08:12:18 -0200 > > "Henrique Dallazuanna" wrote: > > > > > >> Try this also: > >> > >> substr(basename(myfile), 1, nchar(basename(myfile)) - 4) > >>

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Wacek Kusnierczyk
Berwin A Turlach wrote: > G'day Wacek, > > > >>> Or, in case that the extension has more than three letters or >>> "myfile" is a vector of names: >>> >>> R> myfile <- "path1/path2/myoutput.txt" >>> R> sapply(strsplit(basename(myfile),"\\."), function(x) >>> R> paste(x[1:(length(x)-1)], collapse=

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Berwin A Turlach
G'day Wacek, On Fri, 09 Jan 2009 14:22:19 +0100 Wacek Kusnierczyk wrote: > > Apparently also a possibility, I guess it can be made to work with > > the original example and my extensions. > > > > i guess it does work with the original example and your extensions. And I thought that you woul

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Prof Brian Ripley
On Fri, 9 Jan 2009, Berwin A Turlach wrote: G'day Wacek, On Fri, 09 Jan 2009 12:52:46 +0100 Wacek Kusnierczyk wrote: Berwin A Turlach wrote: G'day all, On Fri, 9 Jan 2009 08:12:18 -0200 "Henrique Dallazuanna" wrote: Try this also: substr(basename(myfile), 1, nchar(basename(myfile)) -

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Wacek Kusnierczyk
Berwin A Turlach wrote: > G'day Wacek, > > On Fri, 09 Jan 2009 14:22:19 +0100 > Wacek Kusnierczyk wrote: > > >>> Apparently also a possibility, I guess it can be made to work with >>> the original example and my extensions. >>> >>> >> i guess it does work with the original example and

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Wacek Kusnierczyk
Prof Brian Ripley wrote: > > Actually, that's a valid regex in any of the variants offered. A more > conventional writing of it is the second of > >> f <- 'foo.bar.R' >> sub("[.][^.]*$", "", f) > [1] "foo.bar" >> sub("\\.[^.]*$", "", f) > [1] "foo.bar" > more conventional in r, perhaps. it's not

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Berwin A Turlach
G'day Wacek, On Fri, 09 Jan 2009 15:19:46 +0100 Wacek Kusnierczyk wrote: > i think i did not suggest the original poster to learn perl. As I see it, you didn't suggest anything to the original poster, at least not directly. But, since these days you have to be subscribed to r-help to post IIRC

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Henrique Dallazuanna
Right, Other option is: substr(nameFile, 1, tail(unlist(gregexpr("\\.", nameFile)), 1) - 1) On Fri, Jan 9, 2009 at 1:23 PM, Rau, Roland wrote: > Hi, > > > [mailto:r-help-boun...@r-project.org] On Behalf Of Henrique > > Dallazuanna > > > > Try this also: > > > > substr(basename(myfile), 1, nc

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Duncan Murdoch
On 1/8/2009 9:10 PM, Gundala Viswanath wrote: Dear all, The basename() function returns the extension also: myfile <- "path1/path2/myoutput.txt" basename(myfile) [1] "myoutput.txt" Is there any other function where it just returns plain base: "myoutput" i.e. without 'txt' I'm curious ab

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Rau, Roland
Hi, > [mailto:r-help-boun...@r-project.org] On Behalf Of Henrique > Dallazuanna > > Try this also: > > substr(basename(myfile), 1, nchar(basename(myfile)) - 4) > This, of course, assumes that the extensions are always 3 characters. Sometimes there might be more ("index.html"), sometimes less

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Gabor Grothendieck
On Fri, Jan 9, 2009 at 10:23 AM, Rau, Roland wrote: > Hi, > >> [mailto:r-help-boun...@r-project.org] On Behalf Of Henrique >> Dallazuanna >> >> Try this also: >> >> substr(basename(myfile), 1, nchar(basename(myfile)) - 4) >> > > This, of course, assumes that the extensions are always 3 characters.

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Peter Dalgaard
Duncan Murdoch wrote: > On 1/8/2009 9:10 PM, Gundala Viswanath wrote: >> Dear all, >> >> The basename() function returns the extension also: >> >>> myfile <- "path1/path2/myoutput.txt" >>> basename(myfile) >> [1] "myoutput.txt" >> >> >> Is there any other function where it just returns >> plain bas

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Marc Schwartz
on 01/09/2009 09:00 AM Duncan Murdoch wrote: > On 1/8/2009 9:10 PM, Gundala Viswanath wrote: >> Dear all, >> >> The basename() function returns the extension also: >> >>> myfile <- "path1/path2/myoutput.txt" >>> basename(myfile) >> [1] "myoutput.txt" >> >> >> Is there any other function where it ju

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Wacek Kusnierczyk
> Duncan Murdoch wrote: > >> >> I'm curious about something: does "file extension" have a standard >> definition? Most (all? I haven't tried them all) of the solutions >> presented in this thread would return an empty string for the "plain >> base" if given the filename ".bashrc". >> r

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Wacek Kusnierczyk
Rau, Roland wrote: > > P.S. Any suggestions how to become more proficient with regular > expressions? The O'Reilly book ("Mastering...")? Whenever I tried > anything more complicated than basic usage (things like ^ $ * . ) in R, > I was way faster to write a new function (like above) instead of fin

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Gabor Grothendieck
On Fri, Jan 9, 2009 at 4:20 PM, Wacek Kusnierczyk wrote: > >> Duncan Murdoch wrote: >> >>> >>> I'm curious about something: does "file extension" have a standard >>> definition? Most (all? I haven't tried them all) of the solutions >>> presented in this thread would return an empty string for th

Re: [R] Extracting File Basename without Extension

2009-01-09 Thread Gabor Grothendieck
On Fri, Jan 9, 2009 at 4:28 PM, Wacek Kusnierczyk wrote: > Rau, Roland wrote: >> >> P.S. Any suggestions how to become more proficient with regular >> expressions? The O'Reilly book ("Mastering...")? Whenever I tried >> anything more complicated than basic usage (things like ^ $ * . ) in R, >> I w

Re: [R] Extracting File Basename without Extension

2009-01-11 Thread Wacek Kusnierczyk
Gabor Grothendieck wrote: > On Fri, Jan 9, 2009 at 4:20 PM, Wacek Kusnierczyk > > >> right; there's a straightforward fix to my solution that accounts for >> cases such as '.bashrc': >> >> names = c("foo.bar", ".zee") >> sub("(.+)[.][^.]+$", "\\1", names) >> >> you could also use a lookbehind i

Re: [R] Extracting File Basename without Extension

2009-01-11 Thread Wacek Kusnierczyk
Gabor Grothendieck wrote: > On Fri, Jan 9, 2009 at 4:28 PM, Wacek Kusnierczyk > wrote: > >> Rau, Roland wrote: >> >>> P.S. Any suggestions how to become more proficient with regular >>> expressions? The O'Reilly book ("Mastering...")? Whenever I tried >>> anything more complicated than bas

Re: [R] Extracting File Basename without Extension

2009-01-14 Thread William Dunlap
The S+ basename() function has an argument called suffix and it will remove the suffix from the result. This was based on the Unix basename command, but I missed the special case in the Unix basename that doesn't remove the suffix if the removal would result in an empty string. The suffix must in