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:

 

The YouTube video will be added soon.

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top