In the R programming language, we deal with large amounts of data by representing data in files. In this regard, we can perform certain operations to access data like creating files, reading files, renaming them, etc.
Some different file operations in R are:
It is the first operation that is performed in file handling. R enables us to escape the mutual creation of files, such as it permits us to create runtimes files in a specific location.
file.create("file-name-with-extension")
In this example, we created a text file with the name of EdPresso.txt
with the file.create()
function. Upon successful execution, this method will return the Boolean value true
. Otherwise, it will return false
. In the code below, it will print Congrats! Your File Has been created.
upon successful creation of the file, otherwise it will print Alas! Unable to Create File
.
#creating fileif (file.create("EdPresso.txt")) {print('Congrats! Your File Has been created.')} else {print('Alas! Unable to Create File')}# returns boolean value
Writing to the files is one of the critical data manipulation operations of files in R. It provides a write.table()
function that permits writing the data with respect to a tabular format.
write.table(x= data, file = "file-name-with-extension")
x
: represents the data that we want to writefile
: Indicates the file that has to be writtenWe added ten rows from the ToothGrowth
dataset built-in to the file EdPresso.txt
using the write.table()
function. Form line three and four by reading and printing data on the console using the print()
method.
# calling wirte methodwrite.table(x = ToothGrowth[1:10, ], "EdPresso.txt")data = read.table("EdPresso.txt")print(data) # priting data
After the writing data onto a file, we need to read the information from the file using a built-in function. We use the read.table()
function to read the file’s content that is passed as an argument.
read.table("file-name-to-read-with-extension")
file
: this method takes the file name with extension as parameter.In line three, we are using the read.table()
method to read data from EdPresso.txt
which contains the first ten rows of the ToothGrowth
dataset. Check the code above and print on the console through the print()
method.
len | supp | dose |
4.2 | VC | 0.5 |
11.5 | VC | 0.5 |
7.3 | VC | 0.5 |
5.8 | VC | 0.5 |
6.4 | VC | 0.5 |
10.0 | VC | 0.5 |
11.2 | VC | 0.5 |
11.2 | VC | 0.5 |
5.2 | VC | 0.5 |
7.0 | VC | 0.5 |
write.table(x = ToothGrowth[1:10, ], "EdPresso.txt")# reading data from EdPresso.txtdata = read.table("EdPresso.txt")print(data) # priting data
We can check the file if it exists or not within the current directory or on the mentioned path using the file.exists()
function. We need to pass the file name, and if the file name is in existence, it returns TRUE
. Otherwise, it returns FALSE
.
file.exist("file-name")
In line five, file.exists()
returns TRUE
because EdPresso.txt
exists in the current directory. On the other hand, in line eleven, it returns FALSE
. Why? Educative.txt
does not exist. The content in the EdPresso.txt
file is given below.
EdPresso.txt
Concise shots of dev knowledge
# Checking existance of file# Exist in current dirif (file.exists("EdPresso.txt")) {print('Your File `EdPresso.txt` Exist!')} else {print('Alas! File `EdPresso.txt` is Unavailable')}# Does not existif (file.exists("Educative.txt")) {print('Your File `Educative.txt` Exist!')} else {print('Alas! File `Educative.txt` is Unavailable')}
Instead of creating a file, R also facilitates you to rename files using the file.rename()
function. It neither creates a new file nor changes the file’s content. It only alters the existing file’s name to a new name and returns TRUE
after successful completion. If the file to rename is not found, it terminates with the FALSE
value.
file.rename("old-name", "new-name")
old-name
: Current file name which you want to updatenew-name
: New file name which you want.The code snippet above shows how to use the file.rename()
method in R. Upon success, it prints Successfully updated
otherwise, it shows No such file exists
. Our EdPresso.txt
is below:
EdPresso.txt
Concise shots of dev knowledge
# method to change current file name# Return Falseif (file.rename("EdPresso.txt", "Educative.txt") ) {print("Successfully updated")} else{print("No such file exists")}