Hello, folks! In this article, we would be focusing on an interesting concept of R programming – File Operations in R to know!
Be it any programming language, when it comes to dealing with huge amount of data, we usually need files to represent the data. Regarding files, we often come across situations wherein we need to perform those basic yet essential operations to access data such as reading files, renaming, creation of files, etc.
Table of Contents
Different File Operations in R
Today, we will be focusing on some of the important functions with respect to File operations in R:
- Creation of files
- Renaming existing files
- Writing data to a file
- Reading from a file
- Check for the existence of a file
So, let us begin.
1. Creation of a file in R
The very first operation with respect to file handling is ‘Creation of files’. R enables us to escape the manual creation of files i.e. it allows us to create files at runtime at a particular location.
In the below example, we have created a text file with the name as ‘data.txt’ with
rm(list = ls()) getwd() file.create('data.txt')
After the successful completion of operation, it returns TRUE.
2. Rename files in R
Apart from creating a file, R provides us with
file.rename() function to rename a file at a particular location.
That is, it neither creates a new file nor it alters the file’s content. It only changes the name of the existing file to a new name and returns TRUE after successful completion of task.
If the file to be renamed isn’t found, it terminates with FALSE value.
rm(list = ls()) getwd() file.rename('data.txt','info.txt')
3. Writing to files in R
Now, let us focus on one of the important data manipulation operation of files in R — writing to a file.
R provides us with
write.table() function that writes the data with respect to a tabular format.
In this example, we have added a string to the file ‘info.txt’ using write.table() function.
rm(list = ls()) getwd() write.table('Welcome to the concept of File Handling in R programming!','info.txt')
As a result, the data gets added in a tabular format. That is, in a row-column format, thus it prints ‘1’ as the first row.
4. Check if a File exists or not
With R programming, we can even check for the existence of a file within the system using
file.exists() function. We just need to pass the file name and if the file exists, it returns TRUE, else it terminates with a FALSE value.
rm(list = ls()) getwd() file.exists('info.txt') file.exists('dat.txt')
> file.exists('info.txt')  TRUE > file.exists('dat.txt')  FALSE
5. Reading data from a R file
After writing the data to the file, it is now the time to read information from the file using built-in functions in R.
For the same, R provides us with
read.table() function to read the content of the file passed as argument.
rm(list = ls()) getwd() info = read.table('info.txt') print(info)
Welcome to the concept of File Handling in R programming!
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
Do let us know your understanding about File operations in the comment section.
For more such posts related to R programming, Stay tuned with us.
Till then, Happy Learning! 🙂