Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

R language binary


May 12, 2021 R language tutorial


Table of contents


A binary is a file that contains information stored only in bits and bytes (0 and 1). T hey are not human readable because the bytes in it are converted to characters and symbols that contain many other non-printable characters. T rying to read a binary using any text editor will display characters such as O and .

The binary must be read by a specific program to be used. F or example, a Microsoft Word program's binary can only be read in human readable form through a Word program. T his means that in addition to human-readable text, there is more information, such as the formatting of characters and page numbers, which are stored with alphanumeric characters. T he last binary is a continuous sequence of bytes. T he line breaks we see in the text file are characters that connect the first line to the next line.

Sometimes, data generated by other programs needs to be processed by R as a binary. I n addition, the R language is required to create binary files that can be shared with other programs.

The R language has two functions, WhiteBin() and readBin(), to create and read bins.

Grammar

writeBin(object, con)
readBin(con, what, n )

The following is a description of the parameters used -

  • con is a connection object that reads or writes binary files.

  • Object is the binary to write to.

  • What - like characters, integers, etc. that represent byte mode being read.

  • n The number of bytes read from the binary.

Cases

Let's consider the R language built-in data "mtcars". F irst, we create a csv file from it, convert it to a binary file, and store it as an operating system file. Next we read this created binary.

Write to the binary

We read the data frame "mtcars" as a csv file and write it to the operating system as a binary.

# 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 mode "wb".
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 the binary

The binary created above stores all the data as consecutive bytes. T herefore, we will read it by selecting the appropriate column name value and column value.

# 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 execute the code above, it produces the following results and charts -

 [1]    7108963 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 we can see, we get the raw data by reading the binary in R.