[R] Extracting File Basename without Extension

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jan 9 14:57:34 CET 2009


On Fri, 9 Jan 2009, Berwin A Turlach wrote:

> G'day Wacek,
>
> On Fri, 09 Jan 2009 12:52:46 +0100
> Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
>
>> Berwin A Turlach wrote:
>>> G'day all,
>>>
>>> On Fri, 9 Jan 2009 08:12:18 -0200
>>> "Henrique Dallazuanna" <wwwhsd at gmail.com> 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> sapply(strsplit(basename(myfile),"\\."), function(x)
>>> R> paste(x[1:(length(x)-1)], collapse="."))
>>> [1] "myoutput"
>>> R> myfile2 <- c(myfile, "path2/path3/myoutput.temp")
>>> R> sapply(strsplit(basename(myfile2),"\\."), function(x)
>>> R> paste(x[1:(length(x)-1)], collapse="."))
>>> [1] "myoutput" "myoutput"
>>> R> myfile3 <- c(myfile2, "path4/path5/my.out.put.xls")
>>> R> sapply(strsplit(basename(myfile3),"\\."), function(x)
>>> R> paste(x[1:(length(x)-1)], collapse="."))
>>> [1] "myoutput"   "myoutput"   "my.out.put"

using fixed = TRUE and not escaping '.' is slightly more efficient.

>> 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))
>
> Apparently also a possibility, I guess it can be made to work with the
> original example and my extensions.
>
> Though, it seems to require the knowledge of perl, or at least perl's
> regular expression.

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"

It is the last that is used at various points in R's own code 
(although sometimes with restrictions on what an 'extension' is, e.g.

sub("\\.[[:alnum:]]*$", "", f)

appears in the current SHLIB code.)

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list