A binary file is a file that stores information in the form of bits and bytes (0’s and 1’s).
Binary files are not human-readable because the bytes they contain translate to characters and symbols that have many other non-printable characters.
The text editor shows any binary file as characters such as Ø and ð. Binary files must be read by programs to be useable.
Sometimes, the data generated by other programs must be processed by R as a binary file. R is therefore required to create binary files that can be shared with other programs.
There are two methods to read and write binary files in R: WriteBin()
and ReadBin()
.
The binary file of the Microsoft Word program .doc extension
is only readable by Word Processor. Besides the human-readable text, there is other information, such as the formatting of characters and page numbers. This information is stored with alphanumeric characters.
The binary file is a contiguous sequence of bits of 1’s and 0’s, so all of the information is in the form of 1’s and 0’s.
A binary file stores all of its data as contiguous bytes. To read a binary file, we select appropriate values of column names and column values. We use the file name and connection mode rb
to create the connection opening.
rb
: the mode of connection opening.r
means to read andb
means binary.
We can use the following syntax to read from a binary file:
readBin(con, what, n )
con
: A connection object to read or write the binary file.what
: A mode, such as a character or an integer, that represents the bytes to be read.n
: The number of bytes to read from the binary file.In this code, we read a txt
file file.txt
as a binary file. In line 7, we read the ID. In line 9, we read the Name. Finally, we print the data on the console.
# Read column values# n = 6 as we have 4 values and 2 column namescon = file("myfile.txt", "rb")bindata = readBin(con, integer(), n = 6)# Read the ID values# as first 0:3 byte for col nameID = bindata[0:3]# Similarly 4 to 10 byte for values of name columnName = bindata[4:9]# Combining all the values and make it a data framefinaldata = cbind(ID, Name)print(finaldata)
To write binary files in R, we simply pass the following:
object
: Contains data values.con
: Pass after we open a connection with wb
mode.The opened connection can be closed with the
close(con)
method.
wb
: mode of connection opening.w
means to write andb
means binary.
We can use the following syntax to write a binary file:
writeBin(object, con)
con
: A connection object to read or write the binary file.object
: The binary file that will be written.This demo code creates an employee data frame and initializes the con
object with the output file name myfile.txt
in wb
mode. After this, we write to the file with the writeBin()
method. We pass employee id
, name
, age
, designation
, and salary
as a vector, as well as
stream connection object con
.
# Creating an employee data frameemployee = data.frame("ID" = c(1, 2, 3, 4),"Name" = c("Allen", "Hallen", "Robert", "Joesef"),"Age" = c(19, 31, 23, 48),"Designation" = c("Manager", "Business Analysis", "Project Manager", "Software Engineer"),"Salary" = c(676080, 767800, 811000, 159010))# Creating a connection object using mode "wbcon = file("myfile.txt", "wb")# Writing column names over connection con objectwriteBin(colnames(employee), con)# Writing on file in sequencewriteBin(c(employee$ID, employee$Name, employee$Age, employee$Designation, employee$Salary), con)# Close the connection objectclose(con)