Skip to content
Related Articles

Related Articles

Improve Article

File Handling in R Programming

  • Last Updated : 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.

Creating a File

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 directory
file.create("GFG.txt")

Output:

[1] TRUE

Writing into a File

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 file
file: indicates the name of the file that has to be written

Example:




# Write iris dataset
# into the txt file
write.table(x = iris[1:10, ], 
           file = "GFG.txt")

Output:
output screen

Renaming a File

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 path
to: indicates new file name or path

Example:




# Rename file GFG.txt to newGFG.txt
file.rename("GFG.txt", "newGFG.txt")

Output:

[1] TRUE

Check Existence of a File

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>txt
file.exists("GFG.txt")
  
# Check for newGFG.txt
file.exists("newGFG.txt")

Output:

[1] FALSE
[1] TRUE

Reading a File

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 file
new.iris <- read.table(file = "GFG.txt")
  
# Print
print(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

List all Files

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 path
To know about more optional parameters, use below command in console: help(“list.files”)

Example:




# Show all files in
# current working directory
list.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"

Copy a File

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 copied
to: indicates the path where it has to be copied
To know about more optional parameters, use below command in console: help(“file.copy”)



Example:




# Copying
file.copy("C:/Users/Utkarsh/Documents/
           newGFG.txt", "E:/")
  
# List the files in E:/ drive
list.files("E:/")

Output:

[1] TRUE
[1] "$RECYCLE.BIN"                    "Counter-Strike Global Offensive"
[3] "Home"                            "newGFG.txt"                     
[5] "System Volume Information"

Create a Directory

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 directory
dir.create("GFG")
  
# List files
list.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" 



My Personal Notes arrow_drop_up
Recommended Articles
Page :