How can I rename files in R, and what are some examples of how to do so?

How can I rename files in R, and what are some examples of how to do so?

Renaming files in R is a simple and efficient process that allows you to change the names of your files according to your specific needs. This can be useful when organizing and managing large datasets or when working on multiple projects. To rename files in R, you can use the “file.rename” function, which takes in two arguments – the current file name and the desired new file name. Some examples of how to use this function include changing the file extension, adding a prefix or suffix to the file name, or completely renaming the file. Additionally, you can also use the “dir.rename” function to rename multiple files at once. Overall, renaming files in R provides a convenient way to customize file names and improve overall organization and efficiency in your data analysis.

Rename Files in R (With Examples)


You can use the following methods to rename files in R:

Method 1: Rename One File

file.rename(from='old_name.csv', to='new_name.csv')

Method 2: Replace Pattern in Multiple Files

file.rename(list.files(pattern ='old'),
            str_replace(list.files(pattern='old'), pattern='old', 'new'))

The following examples show how to use each method in practice.

Example: Rename One File

Suppose we have a folder with four CSV files in R:

#display all files in current working directory
list.files()

"data1.csv"  "data2_good.csv"  "data3_good.csv"  "data4_good.csv"

We can use the following code to rename the file called data1.csv to data1_good.csv:

#rename one file
file.rename(from='data1.csv', to='data1_good.csv')

#display all files in current working directory
list.files()

"data1_good.csv"  "data2_good.csv"  "data3_good.csv"  "data4_good.csv"

Notice that the file has been successfully renamed.

Example: Replace Pattern in Multiple Files

Suppose we have a folder with four CSV files in R:

#display all files in current working directory
list.files()

"data1_good.csv"  "data2_good.csv"  "data3_good.csv"  "data4_good.csv"

We can use the following code to replace “good” with “bad” in the name of every single file:

library(stringr)

file.rename(list.files(pattern ='good'),
            str_replace(list.files(pattern='good'), pattern='good', 'bad'))

#display all files in current working directory
list.files()

"data1_bad.csv"  "data2_bad.csv"  "data3_bad.csv"  "data4_bad.csv"

Related: 

Additional Resources

The following tutorials explain how to perform other common operations with files in R:

Cite this article

stats writer (2024). How can I rename files in R, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-rename-files-in-r-and-what-are-some-examples-of-how-to-do-so/

stats writer. "How can I rename files in R, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-rename-files-in-r-and-what-are-some-examples-of-how-to-do-so/.

stats writer. "How can I rename files in R, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-rename-files-in-r-and-what-are-some-examples-of-how-to-do-so/.

stats writer (2024) 'How can I rename files in R, and what are some examples of how to do so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-rename-files-in-r-and-what-are-some-examples-of-how-to-do-so/.

[1] stats writer, "How can I rename files in R, and what are some examples of how to do so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I rename files in R, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top