Saturday, November 28, 2020

How to use R Binary File?

 A 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:

  1. 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

  1. # Reading the "mtcars" data frame as a csv file and will store only the columns "cyl", "am" and "gear".  
  2. write.table(mtcars, file = "mtcars.csv",row.names = FALSEna = "",   
  3. col.names = TRUEsep = ",")  
  4.   
  5. # Storing 5 records from the csv file as a new data frame.  
  6. new.mtcars <- read.table("mtcars.csv",sep = ",",header = TRUE,nrows = 5)  
  7. new.mtcars  
  8.   
  9. # Creating a connection object to write the binary file using mode "wb".  
  10. write.filename = file("/Users/ajeet/R/binary.bin", "wb")  
  11.   
  12. # Writing the column names of the data frame to the connection object.  
  13. writeBin(colnames(new.mtcars), write.filename)  
  14.   
  15. # Writing the records in each of the column to the file.  
  16. writeBin(c(new.mtcars$cyl,new.mtcars$am,new.mtcars$gear), write.filename)  
  17.   
  18. # Closing the file for writing so that other programs can read it.  
  19. 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:

  1. 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

  1. # Creating a connection object to read the file in binary mode using "rb".  
  2. read.filename <- file("/Users/ajeet/R/binary.bin", "rb")  
  3.   
  4. # Reading the column names. n = 3 as we have 3 columns.  
  5. column.names <- readBin(read.filename, character(),  n = 3)  
  6.   
  7. # Reading the column values. n = 18 as we have 3 column names and 15 values.  
  8. read.filename <- file("/Users/ajeet/R/binary.bin", "rb")  
  9. bin_data <- readBin(read.filename, integer(),  n = 18)  
  10.   
  11. # Printing the data.  
  12. print(bin_data)  
  13.   
  14. # Reading the values from 4th byte to 8th byte, which represents "cyl."  
  15. cyl_data = bin_data[4:8]  
  16. print(cyl_data)  
  17.   
  18. # Reading the values form 9th byte to 13th byte which represents "am".  
  19. am_data = bin_data[9:13]  
  20. print(am_data)  
  21.   
  22. # Reading the values form 9th byte to 13th byte which represents "gear".  
  23. gear_data = bin_data[14:18]  
  24. print(gear_data)  
  25.   
  26. # Combining all the read values to a dat frame.  
  27. final_data = cbind(cyl_data, am_data, gear_data)  
  28. colnames(final_data) = column.names  
  29. print(final_data)  

Output

R Binary File

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...