File Operations in R – 5 operations to know!

Filed Under: R Programming
File Operations In R

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.

Different File Operations in R

Today, we will be focusing on some of the important functions with respect to File operations in R:

  1. Creation of files
  2. Renaming existing files
  3. Writing data to a file
  4. Reading from a file
  5. 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.

Syntax:

file.create(file-name-with-extension)

Example:

In the below example, we have created a text file with the name as ‘data.txt’ with file.create() function.

rm(list = ls())
getwd()
file.create('data.txt')

Output:

After the successful completion of operation, it returns TRUE.

[1] 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.

Syntax:

file.rename(old-file-name,new-file-name)

Example:

rm(list = ls())
getwd()
file.rename('data.txt','info.txt')

Output:

[1] TRUE

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.

Syntax:

write.table(data, file-name)

Example:

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')

Output:

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.

Write To A R File
Write To A R File

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.

Syntax:

file.exists(file-name)

Example:

rm(list = ls())
getwd()
file.exists('info.txt')
file.exists('dat.txt')

Output:

> file.exists('info.txt')
[1] TRUE
> file.exists('dat.txt')
[1] 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.

Syntax:

read.table(file-name)

Example:

rm(list = ls())
getwd()
info = read.table('info.txt')
print(info)

Output:

Welcome to the concept of File Handling in R programming!

Conclusion

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! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content