R Advanced

file.rename(): Renaming Single and Multiple Files in R

To rename a file in R, you can use the file.rename() function. It renames a single file or multiple files simultaneously from an old name to a new one.

It returns a logical value. The value is TRUE if the file was successfully renamed, and FALSE if it was not.

Syntax

file.rename(from, to)

Parameters

Argument Description
from It represents a character vector that contains all the files to be renamed.
to It represents a character vector that contains new names for files.

Renaming a single file

Before renaming a file, we need to check if the file exists.

To check if the file exists, use the file.exists() method.

Let’s say we have a directory named “data” and in that directory, we have two files.

  1. Pro.R
  2. old.R

Now, we want to rename the file of old.R to new.R. 

The Pro.R is our R program file, where we write code.

To list files in the current working directory, use the list.files() method.

Write this method inside the Pro.R file.

list.files()

# Output: Pro.R old.R

Check out this screenshot to verify that we have only two files.

Write a code inside Pro.R file that renames the file from the old.R to new.R.

old_file <- "old.R"
new_file <- "new.R"

if (file.exists(old_file)) {
  file.rename(old_file, new_file)
} else {
  cat("The file does not exist")
}

list.files()

# Output:

# [1] TRUE
# [1] "Pro.R" "new.R"

It returns TRUE, indicating that the file has been successfully renamed to data.R.

The file.rename() function will only work if the current file exists and is not currently open. If the file is open, you must close it before renaming it.

Let’s check out the screenshot below to confirm that we successfully renamed a single file.

Renaming multiple files

To rename multiple files, use the file.rename() with a vector of old names and a vector of new names.

You can also use the list.files() with a pattern argument to select files that match a certain pattern.

Let’s say we have a newDir directory with four files.

To list all the files in the current directory, use the list.files() function.

Write this function inside the fourth Pro.R file, as seen in the above screenshot.

list.files()

# Output: [1] "Pro.R" "main_app.R" "main_data.R" "main_index.R"

Now, we will rename three files (“main_app.R”, “main_data.R”, and “main_index.R”) using the file.rename() method.

Write the code below inside the Pro.R file.

library(stringr)

file.rename(list.files(pattern = "main"), 
str_replace(list.files(pattern = "main"), pattern = "main", "my"))

list.files()

# Output:
# [1] TRUE TRUE TRUE
# [1] "Pro.R" "my_app.R" "my_data.R" "my_index.R"

We successfully changed the names of three files, and now verify by going to the file system.

Renaming a file with the full path specified

In this case, you need to pass the full path of the source and destination.

current_name <- "/Users/krunal/Documents/old_name.csv"

new_name <- "/Users/krunal/Documents/new_name.csv"

file.rename(current_name, new_name)

That’s it.

Recent Posts

R prop.table() Function

The prop.table() function in R calculates the proportion or relative frequency of values in a…

10 hours ago

exp() Function: Calculate Exponential of a Number in R

The exp() is a built-in function that calculates the exponential of its input, raising Euler's…

11 hours ago

R split() Function: Splitting a Data

The split() function divides the input data into groups based on some criteria, typically specified…

1 week ago

colMeans(): Calculating the Mean of Columns in R Data Frame

The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…

2 weeks ago

rowMeans(): Calculating the Mean of rows of a Data Frame in R

The rowMeans() is a built-in, highly vectorized function in R that computes the arithmetic mean…

3 weeks ago

colSums(): Calculating the Sum of Columns of a Data Frame in R

The colSums() function in R calculates the sums of columns for numeric matrices, data frames,…

3 weeks ago