A binary file is a file that contains information stored only in form
of bits and bytes.(0’s and 1’s). They are not human readable as the
bytes in it translate to characters and symbols which contain many other
non-printable characters. Attempting to read a binary file using any
text editor will show characters like Ø and ð.
The binary file has to be read by specific programs to be useable. For example, the binary file of a Microsoft Word program can be read to a human readable form only by the Word program. Which indicates that, besides the human readable text, there is a lot more information like formatting of characters and page numbers etc., which are also stored along with alphanumeric characters. And finally a binary file is a continuous sequence of bytes. The line break we see in a text file is a character joining first line to the next.
Sometimes, the data generated by other programs are required to be processed by R as a binary file. Also R is required to create binary files which can be shared with other programs.
R has two functions WriteBin() and readBin() to create and read binary files.
The binary file has to be read by specific programs to be useable. For example, the binary file of a Microsoft Word program can be read to a human readable form only by the Word program. Which indicates that, besides the human readable text, there is a lot more information like formatting of characters and page numbers etc., which are also stored along with alphanumeric characters. And finally a binary file is a continuous sequence of bytes. The line break we see in a text file is a character joining first line to the next.
Sometimes, the data generated by other programs are required to be processed by R as a binary file. Also R is required to create binary files which can be shared with other programs.
R has two functions WriteBin() and readBin() to create and read binary files.
Syntax
writeBin(object, con) readBin(con, what, n )Following is the description of the parameters used −
- con is the connection object to read or write the binary file.
- object is the binary file which to be written.
- what is the mode like character, integer etc. representing the bytes to be read.
- n is the number of bytes to read from the binary file.
Example
We consider the R inbuilt data "mtcars". First we create a csv file from it and convert it to a binary file and store it as a OS file. Next we read this binary file created into R.Writing the Binary File
We read the data frame "mtcars" as a csv file and then write it as a binary file to the OS.# 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)
Reading the Binary File
The binary file created above stores all the data as continuous bytes. So we will read it by choosing appropriate values of column names as well as the column values.# 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 above code, it produces the following result and chart −
[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 3As we can see, we got the original data back by reading the binary file in R.
No comments:
Post a Comment