How can I move a file from one folder to another in R? 2

How can I move a file from one folder to another in R?

Moving a file from one folder to another in R can be achieved using the function “file.rename()”. This function takes two arguments, the current path of the file and the new path where the file should be moved. The file will be moved from its current location to the new location specified. This process can be used to organize files and data within a project or to transfer files between folders. It is a simple and efficient way to manage files in R.

Move File from One Folder to Another in R


You can use the following methods to move a file from one folder to another in R:

Method 1: Move File from One Folder to Another (And Keep Original File)

file.copy(from="C:/Users/bob/Documents/current_data/soccer_data.csv",
          to="C:/Users/bob/Documents/new_data/soccer_data.csv")

This particular syntax copies a file called soccer_data.csv located in the following folder:

C:/Users/bob/Documents/current_data

Then this file is copied into the following folder:

C:/Users/bob/Documents/new_data

Since we used the file.copy() function, the original file is still kept in the folder it came from.

Method 2: Move File from One Folder to Another (And Delete Original File)

file.rename(from="C:/Users/bob/Documents/current_data/soccer_data.csv",
            to="C:/Users/bob/Documents/new_data/soccer_data.csv")

This particular syntax copies a file called soccer_data.csv located in the following folder:

C:/Users/bob/Documents/current_data

Then this file is copied into the following folder:

C:/Users/bob/Documents/new_data

Since we used the file.rename() function, the original file is deleted from the folder it came from.

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

Example 1: Move File in R (And Keep Original File)

Suppose we want to move a file called soccer_data.csv located in the following folder:

C:/Users/bob/Documents/current_data

The folder currently has three files in it:

We can use the following syntax in R to move soccer_data.csv into a new folder called new_data:

file.copy(from="C:/Users/bob/Documents/current_data/soccer_data.csv",
          to="C:/Users/bob/Documents/new_data/soccer_data.csv")

[1] TRUE

The file.copy() function returns a value of TRUE, which tells us it worked.

If we open the new_data folder, we can see that the soccer_data.csv file has indeed been copied into it:

And if we return to the folder where the file came from, we will see that it still exists there.

Note: If either of the file paths is incorrect, the file.copy() function will instead return a value of FALSE.

Example 2: Move File in R (And Delete Original File)

We can use the following syntax in R to move soccer_data.csv into a new folder called new_data and delete the original file:

file.rename(from="C:/Users/bob/Documents/current_data/soccer_data.csv",
            to="C:/Users/bob/Documents/new_data/soccer_data.csv")

[1] TRUE

The file.rename() function returns a value of TRUE, which tells us it worked.

If we open the new_data folder, we can see that the soccer_data.csv file has indeed been copied into it:

And if we return to the folder where the file came from, we will see that it has been deleted from that folder:

Cite this article

stats writer (2024). How can I move a file from one folder to another in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-move-a-file-from-one-folder-to-another-in-r/

stats writer. "How can I move a file from one folder to another in R?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-move-a-file-from-one-folder-to-another-in-r/.

stats writer. "How can I move a file from one folder to another in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-move-a-file-from-one-folder-to-another-in-r/.

stats writer (2024) 'How can I move a file from one folder to another in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-move-a-file-from-one-folder-to-another-in-r/.

[1] stats writer, "How can I move a file from one folder to another in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I move a file from one folder to another in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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