File Handling in R Programming
Last Updated :
18 Jun, 2025
In R programming, handling files (such as reading, writing, creating, and renaming files) can be done using built-in functions available in the base R package. These operations help in managing data stored in files, which is essential for tasks like data analysis, data manipulation, and automation. In this article we will cover various file handling tasks in R, including creating files, writing to them, reading from them, checking file existence, renaming files, and more.
1. Creating a File
In R, we can create a new file using the file.create() function. If the file already exists, it will be truncated (emptied). This function returns a logical value: TRUE if the file is created successfully, and FALSE otherwise.
Syntax:
file.create("file_name")
Parameters:
- "file_name": The name of the file to be created.
Example:
R
Output:
TRUE
2. Writing into a File
To write data into a file, we can use the write.table() function in R. This function is part of the utils package and allows we to write a data frame or matrix to a file.
Syntax:
write.table(x, file)
Parameters:
- x: The object (data frame or matrix) to write to the file.
- file: The name of the file where the data should be written.
Example:
R
write.table(x = iris[1:10, ], file = "GFG.txt")
Output:
Writing into a text file3. Renaming a File
To rename a file, use the file.rename() function. It returns a logical value: TRUE if the file is successfully renamed, and FALSE otherwise.
Syntax:
file.rename(from, to)
Parameters:
- from: The current file name or path.
- to: The new file name or path.
Example:
R
file.rename("GFG.txt", "newGFG.txt")
Output:
TRUE
4. Check Existence of a File
We can check if a file exists in the current working directory using the file.exists() function. It returns TRUE if the file exists, and FALSE otherwise.
Syntax:
file.exists("file_name")
Parameters:
- "file_name": The name of the file we want to check.
Example:
R
file.exists("GFG.txt")
file.exists("newGFG.txt")
Output:
FALSE
TRUE
5. Reading a File
To read a file into R, use the read.table() function. This function reads files and returns the data as a data frame, which can then be used for further computations.
Syntax:
read.table(file)
Parameters:
- file: The name of the file to read.
Example:
R
new.iris <- read.table(file = "GFG.txt")
print(new.iris)
Output:
Reading a File6. List All Files in a Directory
We can list all files in the current working directory (or a specified path) using the list.files() function.
Syntax:
list.files(path)
Parameters:
- path: The directory path. If not specified, it lists the files in the current working directory.
Example:
R
Output:
[1] "Bandicam" "Bluetooth Folder" "GFG.txt" "newGFG.txt" "My Music" "Documents"
7. Copying a File
The file.copy() function allows we to copy files from one location to another.
Syntax:
file.copy(from, to)
Parameters:
- from: The file path of the source file.
- to: The path where the file should be copied.
Example:
R
file.copy("newGFG.txt", "E:/")
list.files("E:/")
Output:
[1] "newGFG.txt" "Documents"
8. Create a Directory
We can create a new directory using the dir.create() function. If no path is specified, the directory is created in the current working directory.
Syntax:
dir.create(path)
Parameters:
- path: The path where the directory should be created.
Example:
R
dir.create("GFG")
list.files()
Output:
[1] "Bandicam" "GFG" "Documents" "My Music"