Remove Extension from File Name in R (Example)
In this article, I’ll demonstrate how to get file names without extension in the R programming language.
The article contains one example for the extraction of file names without extensions. To be more specific, the table of content is structured like this:
Let’s jump right to the R code!
Creation of Example Data
Have a look at the example file names below:
my_files <- c("data1.csv", "data2.xlsx", "data3.txt") # Example file names my_files # Print file names # [1] "data1.csv" "data2.xlsx" "data3.txt"
As you can see based on the previously shown output of the RStudio console, we have created a vector of file names with extensions. Note that each file name has a different extension, i.e. csv, xlsx, and txt.
Example: Extract File Names without Extensions Using file_path_sans_ext() Function
This example demonstrates how to delete extensions from a file name in R.
For this task, we can apply the file_path_sans_ext function provided by the tools package:
tools::file_path_sans_ext(my_files) # Remove file extensions # [1] "data1" "data2" "data3"
Have a look at the previous output: It shows all our file names without file extensions.
Video, Further Resources & Summary
Do you need further info on the R code of this article? Then I recommend watching the following video instruction on my YouTube channel. I’m explaining the contents of this article in the video:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might want to read the other tutorials on this homepage. You can find some related tutorials on similar topics such as extracting data, directories, and naming data below.
Summary: This tutorial has illustrated how to extract file names without extensions in R. If you have additional questions, let me know in the comments section. Furthermore, don’t forget to subscribe to my email newsletter for regular updates on new articles.