R Binary File

R Binary File

R binary file is a file which contains information present only in the form of bits and bytes(0’s and 1’s). They are not human-readable because the bytes translate into characters and symbols that contain many other non-printable characters. If we will read a binary file using any text editor, it will show the characters like ð and Ø.

The code is relatively very easy to read binary data into R. To read binary data, we must know how a piece of information has been parsed into binary.

The binary file must be read by specific programs to be useful. For example, the binary file of a Microsoft Word program can only be read by the Word program in a human-readable form. It indicates that, in addition to human-readable text, there is a lot of information such as character formatting and page numbers, etc., which are also stored with alphanumeric characters. And finally, a binary file is a contiguous sequence of bytes. The line break we see in a text file is a character joining the first line to the next line.

Sometimes, the data generated by other programs need to be processed by R as a binary file. Also, R needs to create binary files that can be shared with other programs. There are two functions writeBin () and readBin () for creating and reading binary files in R.

R Binary File

Writing the Binary File

Like CSV and Excel files, we can also write into a binary file. R provides a writeBin() function for writing the data into a binary file. There is the following syntax of writeBin() function:

writeBin(object,con)  

Here,

  • The ?con’ is the connection object which is used to write the binary file.
  • The ?object’ is the binary file in which we write our data.

Let’s see an example to understand how this function is used to write data into a file in binary format. In the following example, we will use R inbuilt data “mtcars.” We will create a CSV file from it and convert it into a binary file.

Example

# Reading the "mtcars" data frame as a csv file and will store only the columns "cyl", "am" and "gear".  
write.table(mtcars, file = "mtcars.csv",row.names = FALSE, na = "",   
col.names = TRUE, sep = ",")  
  
# Storing 5 records from the csv file as a new data frame.  
new.mtcars <- read.table("mtcars.csv",sep = ",",header = TRUE,nrows = 5)  
new.mtcars  
  
# Creating a connection object to write the binary file using mode "wb".  
write.filename = file("/Users/ajeet/R/binary.bin", "wb")  
  
# Writing the column names of the data frame to the connection object.  
writeBin(colnames(new.mtcars), write.filename)  
  
# Writing the records in each of the column to the file.  
writeBin(c(new.mtcars$cyl,new.mtcars$am,new.mtcars$gear), write.filename)  
  
# Closing the file for writing so that other programs can read it.  
close(write.filename)  

Output

R Binary File
R Binary File

Reading the Binary File

We can also read our binary file which we have created before. For this purpose, R provides a readBin() function for reading the data from a binary file.

There is the following syntax of readbin() function:

readBin(con,what,n)  

Here,

  • The ?con’ is the connection object which is used to read the binary file.
  • The ?what’ is the mode such as character, integer, etc. which represent the bytes to be read.
  • The ?n’ is the number of bytes which we want to read from the binary file.

Let’s see an example in which we read our binary data from binary.bin file.

Example

# Creating a connection object to read the file in binary mode using "rb".  
read.filename <- file("/Users/ajeet/R/binary.bin", "rb")  
  
# Reading the column names. n = 3 as we have 3 columns.  
column.names <- readBin(read.filename, character(),  n = 3)  
  
# Reading the column values. n = 18 as we have 3 column names and 15 values.  
read.filename <- file("/Users/ajeet/R/binary.bin", "rb")  
bin_data <- readBin(read.filename, integer(),  n = 18)  
  
# Printing the data.  
print(bin_data)  
  
# Reading the values from 4th byte to 8th byte, which represents "cyl."  
cyl_data = bin_data[4:8]  
print(cyl_data)  
  
# Reading the values form 9th byte to 13th byte which represents "am".  
am_data = bin_data[9:13]  
print(am_data)  
  
# Reading the values form 9th byte to 13th byte which represents "gear".  
gear_data = bin_data[14:18]  
print(gear_data)  
  
# Combining all the read values to a dat frame.  
final_data = cbind(cyl_data, am_data, gear_data)  
colnames(final_data) = column.names  
print(final_data)  

Output

R Binary File

Next Topic : Click Here

This Post Has One Comment

Leave a Reply