[R] Extracting File Basename without Extension

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Fri Jan 9 17:01:10 CET 2009


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 base:
>>
>> "myoutput"
>>
>> i.e. without 'txt'
> 
> 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".
> 
> Windows (where file extensions really mean something), though reluctant
> to create such a file, appears to agree that the extension is bashrc,
> even though to me it appears clear that that file has no extension.

I'm not sure what is clear about it, but the GNU utility agrees with you:

>basename abc/.exe .exe
.exe
>basename abc/1.exe .exe
1

Anyone want to contribute code for an optional suffix= argument for R's
basename()?

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907




More information about the R-help mailing list