Re: [R] eliminating string from a char vector

From: Marc Schwartz <>
Date: Sat, 21 Jun 2008 21:34:37 -0500

on 06/21/2008 09:19 PM milton ruser wrote:
> Hi there,
> I have a list of filenames like
> a<-c("file1.dbf", "file32.dbf", "myfile_temp.dbf")
> and I would like to remove the ".dbf" string from all records and obtain
> somethink like
> file1
> file32
> myfile_temp
> Thanks in advance,
> miltinho
> brazil

As is usually the case with R, there are several approaches:

 > sub("\\.dbf", "", a)
[1] "file1" "file32" "myfile_temp"

 > sapply(strsplit(a, "\\."), "[", 1)
[1] "file1" "file32" "myfile_temp"

 > substr(a, 1, nchar(a) - 4)
[1] "file1" "file32" "myfile_temp"

My personal preference would be the first.

See ?sub, ?strsplit, ?sapply, ?substr and ?nchar for more information.

As an aside, if you should need to manipulate complete file paths to extract the file name, see ?basename

HTH, Marc Schwartz mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sun 22 Jun 2008 - 02:36:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 22 Jun 2008 - 03:30:55 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive