[R] Extracting File Basename without Extension

Berwin A Turlach berwin at maths.uwa.edu.au
Fri Jan 9 12:21:02 CET 2009


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) paste(x[1:(length(x)-1)], collapse="."))
[1] "myoutput"
R> myfile2 <- c(myfile, "path2/path3/myoutput.temp")
R> sapply(strsplit(basename(myfile2),"\\."), function(x) 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) paste(x[1:(length(x)-1)], collapse="."))
[1] "myoutput"   "myoutput"   "my.out.put"

HTH.

Cheers,

	Berwin

> On Fri, Jan 9, 2009 at 12:10 AM, Gundala Viswanath
> <gundalav at gmail.com>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'
> >
> > - Gundala Viswanath
> > Jakarta - Indonesia

=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6516 4416 (secr)
Dept of Statistics and Applied Probability        +65 6516 6650 (self)
Faculty of Science                          FAX : +65 6872 3919       
National University of Singapore     
6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
Singapore 117546                    http://www.stat.nus.edu.sg/~statba




More information about the R-help mailing list