File Handling in R Programming

22 Jul, 2020
In R Programming, handling of files such as reading and writing files can be done by using in-built functions present in R base package. In this article, let us discuss reading and writing of CSV files, creating a file, renaming a file, check the existence of the file, listing all files in the working directory, copying files and creating directories.
Using file.create() function, a new file can be created from console or truncates if already exists. The function returns a TRUE logical value if file is created otherwise, returns FALSE.
Syntax:file.create(” “)
Parameters:” “: name of the file is passed in ” ” that has to be created
Example:
# Create a file# The file created can be seen# in your working directoryfile.create("GFG.txt")
Output:
[1] TRUE

write.table() function in R programming is used to write an object to a file. This function is present in utils package in R and writes data frame or matrix object to any type of file.
Syntax:write.table(x, file)
Parameters:x: indicates the object that has to be written into the filefile: indicates the name of the file that has to be written
Example:
# Write iris dataset# into the txt filewrite.table(x = iris[1:10, ],            file = "GFG.txt")
Output:
The file.rename() function renames the file and return a logical value. The function renames files but not directories.
Syntax:file.rename(from, to)
Parameters:from: indicates current file name or pathto: indicates new file name or path
Example:
# Rename file GFG.txt to newGFG.txtfile.rename("GFG.txt", "newGFG.txt")
Output:
[1] TRUE

A file exists or not in current working directory can be checked using file.exists() function. It returns TRUE logical value if file exists, otherwise returns FALSE.
Syntax:file.exists(” “)
Parameters:” “: name of the file that has to be searched in the current working directory is passed in ” ”
Example:
# Check for GFG>txtfile.exists("GFG.txt")  # Check for newGFG.txtfile.exists("newGFG.txt")
Output:
[1] FALSE
[1] TRUE

Using read.table() function in R, files can be read and output is shown as dataframe. This functions helps in analyzing the dataframe for further computations.
Syntax:read.table(file)
Parameters:file: indicates the name of the file that has to be read
Example:
# Reading txt filenew.iris <- read.table(file = "GFG.txt")  # Printprint(new.iris)
    X Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1   1          5.1         3.5          1.4         0.2  setosa
2   2          4.9         3.0          1.4         0.2  setosa
3   3          4.7         3.2          1.3         0.2  setosa
4   4          4.6         3.1          1.5         0.2  setosa
5   5          5.0         3.6          1.4         0.2  setosa
6   6          5.4         3.9          1.7         0.4  setosa
7   7          4.6         3.4          1.4         0.3  setosa
8   8          5.0         3.4          1.5         0.2  setosa
9   9          4.4         2.9          1.4         0.2  setosa
10 10          4.9         3.1          1.5         0.1  setosa

Using list.files() function, all files of specified path will be shown in the output. If path is not passed in the function parameter, files present in current working directory is shown as output.
Syntax:list.files(path)
Parameters:path: indicates the pathTo know about more optional parameters, use below command in console: help(“list.files”)
Example:
# Show all files in# current working directorylist.files()
Output:
 [1] "Bandicam"                "Bluetooth Folder"       
 [3] "Book1.xlsx"              "Custom Office Templates"
 [5] "desktop.ini"             "list.xlsx"              
 [7] "My Music"                "My Pictures"            
 [9] "My Videos"               "newGFG.txt"             
[11] "Visual Studio 2012"

The file.copy() function in R helps to create a copy of specified file from console itself.
Syntax:file.copy(from, to)
Parameters:from: indicates the file path that has to be copiedto: indicates the path where it has to be copiedTo know about more optional parameters, use below command in console: help(“file.copy”)
Example:
# Copyingfile.copy("C:/Users/Utkarsh/Documents/           newGFG.txt", "E:/")  # List the files in E:/ drivelist.files("E:/")
Output:
[1] TRUE
[1] "$RECYCLE.BIN"                    "Counter-Strike Global Offensive"
[3] "Home"                            "newGFG.txt"                     
[5] "System Volume Information"

The dir.create() function creates a directory in the path specified in the function parameter. If path is not specified in function parameter, directory is created in current working directory.
Syntax:dir.create(path)
Parameters:path: indicates the path where directory has to be created with directory name at the end of the path
Example:
# Without specifying the path,# directory will be created in # current working directorydir.create("GFG")  # List fileslist.files()
Output:
 [1] "Bandicam"                "Bluetooth Folder"       
 [3] "Book1.xlsx"              "Custom Office Templates"
 [5] "desktop.ini"             "GFG"                    
 [7] "list.xlsx"               "My Music"               
 [9] "My Pictures"             "My Videos"              
[11] "newGFG.txt"              "Visual Studio 2012" 

Picked
R-FileHandling
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.