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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.