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.
file.rename(from, to)
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. |
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.
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.
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.
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.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
The prop.table() function in R calculates the proportion or relative frequency of values in a…
The exp() is a built-in function that calculates the exponential of its input, raising Euler's…
The split() function divides the input data into groups based on some criteria, typically specified…
The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…
The rowMeans() is a built-in, highly vectorized function in R that computes the arithmetic mean…
The colSums() function in R calculates the sums of columns for numeric matrices, data frames,…