In the computer science world, text files contain data that can easily be understood by humans. It includes letters, numbers, and other characters. On the other hand, binary files contain 1s and 0s that only computers can interpret. The information stored in a binary file can’t be read by humans as the bytes in it translate to characters and symbols that contain various other non-printable characters.
It may happen some times when the data produced by other programs are essential to be processed by the R language as a binary file. Furthermore, R is necessarily responsible for creating binary files that can be shared with other programs. The four most important operations that can be performed in a binary file are:
- Creating and writing to a binary file
- Reading from the binary file
- Append to the binary file
- Deleting the binary file
Creating and writing to a binary file
The both creating and writing to a binary file can be performed by a single function
writeBin() by opening the file in “wb” mode where w indicates write and b indicates binary mode.
Syntax: writeBin(object, con)
object: an R object to be written to the connection
con: a connection object or a character string naming a file or a raw vector.
Reading from the binary file
Reading from the binary file can be performed by a the function
readBin() by opening the file in “rb” mode where r indicates read and b indicates binary mode.
Syntax: readBin(con, what, n )
con: a connection object or a character string naming a file or a raw vector
what: either an object whose mode will give the mode of the vector to be read or a character vector of length one describing the mode: one of “numeric”, “double”, “integer”, “int”, “logical”, “complex”, “character”, “raw”
n: the (maximal) number of records to be read
ID Name Age Pin [1, ] 0 0 0 0 [2, ] 1072693248 1074266112 1074790400 1073741824 [3, ] 0 0 0 0 [4, ] 1073741824 1074790400 1074266112 1072693248
Append to the binary file
Append to the binary file can be performed by a the same function
writeBin() by opening the file in “ab” mode where a indicates append and b indicates binary mode.
Delete the binary file
In R, one can delete the binary file by using
file.remove() command and then unlink the deleted file by using
- Working with CSV files in R Programming
- Working with XML Files in R Programming
- Working with Excel Files in R Programming
- Working with JSON Files in R Programming
- Working with Text Files in Julia
- Working with Databases in R Programming
- Data Wrangling in R Programming - Working with Tibbles
- List all the Objects present in the Current Working Directory in R Programming - ls() Function
- Reading Files in R Programming
- Reading Tabular Data from files in R Programming
- Writing to Files in R Programming
- Convert an Integer to a Binary value in R Programming - as.binary() Function
- Internal working of Set in Python
- Internal working of list in Python
- Advantages and Disadvantages of working in a Startup
- Working With JSON Data in Python
- How to Get the Current Working Directory in Golang?
- How to align objects vertically when working with grids in CSS ?
- Working on BitBucket using Git
- Working on Git for GUI
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.