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)
Parameters:
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.
Example:
Python3
df = data.frame(
"ID" = c( 1 , 2 , 3 , 4 ),
"Name" = c( "Tony" , "Thor" , "Loki" , "Hulk" ),
"Age" = c( 20 , 34 , 24 , 40 ),
"Pin" = c( 756083 , 756001 , 751003 , 110011 )
)
con = file ( "myfile.dat" , "wb" )
writeBin(colnames(df), con)
writeBin(c(df$ ID , df$Name, df$Age, df$Pin), con)
close(con)
|
Output:

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 )
Parameters:
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
Example:
Python3
con = file ( "myfile.dat" , "rb" )
colname = readBin(con, character(), n = 4 )
con = file ( "myfile.dat" , "rb" )
bindata = readBin(con, integer(), n = 20 )
ID = bindata[ 5 : 8 ]
Name = bindata[ 9 : 12 ]
Age = bindata[ 13 : 16 ]
PinCode = bindata[ 17 : 20 ]
finaldata = cbind( ID , Name, Age, PinCode)
colnames(finaldata) = colname
print (finaldata)
|
Output:
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.
Example:
Python3
df = data.frame(
"Salary" = c( 100 , 200 , 300 , 400 ),
"Experience" = c( 3 , 5 , 10 , 4 )
)
con = file ( "myfile.dat" , "ab" )
writeBin(colnames(df), con)
writeBin(df$Salary, con)
close(con)
|
Output:

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 unlink() function.
Example:
Python3
fileName < - "myfile.dat"
if ( file .exists(fileName))
file .remove(fileName)
unlink(fileName, recursive = TRUE)
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!