How can I delete a file using R? Can you provide an example? 2

How can I delete a file using R? Can you provide an example?

To delete a file using R, you can use the “file.remove()” function. This function takes a file path as its argument and permanently deletes the file from the specified location. An example of using this function would be: file.remove(“C:/Documents/example.txt”), which would delete the file “example.txt” from the “Documents” folder located in the C drive. It is important to note that this function does not move the file to the recycle bin, so the file will be permanently deleted without the possibility of retrieval.

Delete a File Using R (With Example)


You can use the following syntax to delete a file in a specific location using R:

#define file to delete
this_file  <- "C:/Users/bob/Documents/my_data_files/soccer_data.csv"

#delete file if it exists
if (file.exists(this_file)) {
  file.remove(this_file)
  cat("File deleted")
} else {
  cat("No file found")
}

This particular syntax attempts to delete a file called soccer_data.csv located in the following folder:

C:/Users/bob/Documents/my_data_files

If the file exists, the file.remove() function deletes the file and uses the function to output the message “File deleted” to the console.

If the file does not exist, then the cat function outputs the message “No file found” to the console.

The following example shows how to use this syntax in practice.

Example: Delete a File Using R

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

C:/Users/bob/Documents/my_data_files

The folder currently has three files in it:

We can use the following syntax in R to delete this file if it exists:

#define file to delete
this_file  <- "C:/Users/bob/Documents/my_data_files/soccer_data.csv"

#delete file if it exists
if (file.exists(this_file)) {
  file.remove(this_file)
  cat("File deleted")
} else {
  cat("No file found")
}

File deleted

 We receive the message “File deleted” which tells us that the file has been deleted.

If we return to the folder where the file used to exist, we can see that it indeed has been deleted:

Cite this article

stats writer (2024). How can I delete a file using R? Can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-delete-a-file-using-r-can-you-provide-an-example/

stats writer. "How can I delete a file using R? Can you provide an example?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-delete-a-file-using-r-can-you-provide-an-example/.

stats writer. "How can I delete a file using R? Can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-delete-a-file-using-r-can-you-provide-an-example/.

stats writer (2024) 'How can I delete a file using R? Can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-delete-a-file-using-r-can-you-provide-an-example/.

[1] stats writer, "How can I delete a file using R? Can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I delete a file using R? Can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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