DED9

Learning Binary Files In R Programming Language

Binary Files are Files that contain only Information stored in the form of bits and bytes (0 and 1). They are not readable by humans because the bytes in them are translated into characters and symbols that contain many other non-printable characters. 

Attempt to read binary Files using any text editor; it represents characters such as Ø and others.

The binary Files must be read by specific programs to be usable. For example, a Microsoft Word application binary can only be used with Word; it is made legible for humans.
This program, in addition to displaying readable text for humans, includes a lot of other Information, such as formatting characters and page numbers, and the character, which is also stored alongside the alphabetic characters.
Finally, a binary File is a continuous sequence of bytes. This breakline that we see in a text File is a character that connects to the following line.

Sometimes data generated by other programs must be processed by R as a binary File. R must also create a binary File that can be shared with other programs.

In the R programming language, there are two functions for creating and reading binary files: writeBin() and readBin(), respectively.

Syntax

writeBin (object, con)

readBin (con, what, n)

Parameters used in the above code are as follows:

Example

We first consider the “mtcars” dataset, which is an internal R dataset. First, we can create a CSV File from it and convert it to a binary File, then save it as an OS File. Then we read this binary created in R.

Write a Binary File

Binary Files

We read the “mtcars” data frame as a CSV File and then write it as a binary File for the operating System.

# Read the “mtcars” data frame as a csv file and store only the columns

"Cyl", "am" and "gear".

write.table (mtcars, file = “mtcars.csv”, row.names = FALSE, na = “”,

col.names = TRUE, sep = “,”)

# Store 5 records from the csv file as a new data frame.

new.mtcars <- read.table (“mtcars.csv”, sep = “,”, header = TRUE, nrows = 5)

# Create a connection object to write the binary file using “wb” mode.

write.filename = file ("/ web / com / binmtcars.dat", "wb")

# Write the column names of the data frame to the connection object.

writeBin (colnames (new.mtcars), write.filename)

# Write the records in each of the column to the file.

writeBin (c (new.mtcars $ cyl, new.mtcars $ am, new.mtcars $ gear), write.filename)

# Close the file for writing so that it can be read by other program.

close (write.filename)

Read Binary Files

The binary created above stores all the data in the form of continuous bytes. We read that File by selecting the correct name and values ​​for the columns.

# Create a connection object to read the file in binary mode using “rb”.

read.filename <- file ("/ web / com / binmtcars.dat", "rb")

# First read the column names. n = 3 as we have 3 columns.

column.names <- readBin (read.filename, character (), n = 3)

# Next read the column values. n = 18 as we have 3 column names and 15 values.

read.filename <- file ("/ web / com / binmtcars.dat", "rb")

bindata <- readBin (read.filename, integer (), n = 18)

# Print the data.

print (bindata)

# Read the values ​​from 4th byte to 8th byte which represents “cyl”.

cyldata = bindata [4: 8]

print (cyldata)

# Read the values ​​form 9th byte to 13th byte which represents “am”.

amdata = bindata [9:13]

print (amdata)

# Read the values ​​form 9th byte to 13th byte which represents “gear”.

geardata = bindata [14:18]

print (geardata)

# Combine all the read values ​​to a dat frame.

finaldata = cbind (cyldata, amdata, geardata)

colnames (finaldata) = column.names

print (finaldata)

When we run the above code, the following results and diagrams are executable:

[1] 7108996 1728081249 7496037 6 6 4

[7] 6 8 1 1 1 0

[13] 0 4 4 4 3 3

[1] 6 6 4 6 8

[1] 1 1 1 0 0

[1] 4 4 4 3 3

cyl am gear

[1,] 6 1 4

[2,] 6 1 4

[3,] 4 1 4

[4,] 6 0 3

[5,] 8 0 3

As you can see, the primary data is obtained by reading the binary File in R; we have returned.

Exit mobile version