How to Delete a File Using R (Example)

 

This article illustrates how to apply the file.remove function to delete files in R programming.

Table of contents:

Let’s get started:

 

Example Directory & File

In this tutorial, we’ll use the following folder and file to illustrate our R code:

 

example directory and file

 

As you can see in the previous screenshot, our exemplifying working directory contains a TXT file called “my file”.

 

Example: Removing File from Working Directory Using file.remove() Function

This example illustrates how to apply the file.remove function to delete files from a folder from within the R programming language (i.e. RStudio).

For this, we have to specify the path where our file is located and the name of our file:

file.remove("C:/Users/Joach/Desktop/my directory/my file.txt")   # Apply file.remove

After executing the previous R code, our example directory looks like this:

 

directory without file

 

As you can see, we have removed our example file from our working directory, and now it is completely empty.

 

Video & Further Resources

Have a look at the following video of my YouTube channel. I’m showing the R programming codes of the present tutorial in the video.

 

 

Besides the video, you might read the related articles on this homepage.

 

At this point of the article you should know how to remove files from a computer in the R programming language.

In this tutorial, we have illustrated how to use the file.remove function to extract a TXT file. Please note that we could use the same R syntax to remove other types of files such as CSV files, XLSX files, PNG, or JPEG formats as well. We simply have to make sure to specify the correct file extension within the file.remove function.

In case you have any further questions and/or comments, let me know in the comments. Besides that, don’t forget to subscribe to my email newsletter in order to get updates on the newest tutorials.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top