Rename Files Using R (Example)

 

In this tutorial you’ll learn how to change file names of the files in a working directory on your computer in R.

The content looks as follows:

So now the part you have been waiting for – the tutorial!

Example Files & Working Directory

In the example of this tutorial, we’ll use the following path and working directory as basement:

my_path <- "C:/Users/Joach/Desktop/my directory/"  # Define working directory
my_path                                            # Print path to working directory
# [1] "C:/Users/Joach/Desktop/my directory/"

 

example directory

 

The previous screenshot shows how our example directory looks like. It contains four PNG files with different file names.

 

Example: Rename Files Using file.rename() Function in R

This example illustrates how to modify the files stored in our working directory using the R programming language.

First, we have to get the current names of all files in our directory. For this, we can use the list.files function as shown below:

file_names_old <- list.files(my_path)              # Get current file names
file_names_old                                     # Print current file names
# [1] "another file.png"          "i have many png files.png" "this is a file.png"        "what a nice file.png"

Note that the list.file function provides additional arguments, e.g. you may filter file names depending on a specific file extension. However, in this example we simply rename all files in our folder.

As next step, we have to define the new file names that we want to assign. In this example, we’ll specify a running sequence of file names with the prefix “File No”:

file_names_new <- paste0("File No ",               # Define new file names
                         1:length(file_names_old),
                         ".png")
file_names_new                                     # Print new file names
# [1] "File No 1.png" "File No 2.png" "File No 3.png" "File No 4.png"

The previous output of the RStudio console shows the four new file names that we will use.

As a last step, we can now apply the file.rename function to adjust the names of our files:

file.rename(paste0(my_path, file_names_old),       # Rename files
            paste0(my_path, file_names_new))

After running the previous R syntax, the files in our working directory will look like this:

 

renamed files in directory

 

As you can see, all files have been renamed.

 

Video, Further Resources & Summary

If you need more info on the R syntax of this tutorial, you might want to watch the following video of my YouTube channel. In the video, I illustrate the contents of this article in a live programming session.

 

The YouTube video will be added soon.

 

Furthermore, you might want to read the other tutorials on my homepage:

 

Summary: In this R programming tutorial you have learned how to modify PNG file names.

Please note that we could apply the same type of R syntax to rename other file types such as PDF, JPEG, CSV, XLSX, and so on. Furthermore, we could use the R code of this tutorial to rename a large number of files very efficiently.

If you have any additional questions, let me know in the comments section below.

 

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.


3 Comments. Leave new

  • This didn’t work. is there a difference between your version of R and mine? I am using 4.0.5

    Reply
  • I was missing a “slash” — “/” –at the end of “my_path” parameter. Fixed and works now.

    Reply

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