A binary file is a file that contains information (0 and 1) stored only in bits and bytes, which are unreadable because the bytes in it are converted to characters and symbols containing many other non-printable characters, and whatever we try to read a binary file with any text editor will show up as characters like Ø and ð.
But the binary must be read by a specific program before it can be used. For example, the binary files of a Microsoft Word program can only be readable to human-readable forms through the Word program. This shows that there is more information, such as formatted characters and page numbers, in addition to human-readable text, which are also stored with alphanumeric characters. Finally, the binary file is a continuous sequence of bytes. The newline character we see in the text file is the character that connects the first line to the next.
Sometimes, the data generated by other programs needs to be processed by R as a binary file. In addition, R needs to create a binary file that can be shared with other programs. There are two functions in R to create and read binary files, namely: WriteBin() and readBin() functions. Let's see the syntax:
writeBin(object, con) readBin(con, what, n )
The parameters are described as follows:
- con - is the connection object to read or write to a binary file.
- object - is the binary file to be written.
- what - is a pattern like characters, integers, etc., representing the bytes to be read.
- n - is the number of bytes read from a binary file.
We next create a csv file using R built-in data "mtcars" and convert it into a binary file and store it as an operating system file as follows:
#my first R program # Read the "mtcars" data frame as a csv file and store only the columns "cyl", "am" and "gear". (mtcars, file = "", = FALSE, na = "", = TRUE, sep = ",") # Store 5 records from the csv file as a new data frame. <- ("",sep = ",",header = TRUE,nrows = 5) # Create a connection object to write the binary file using mode "wb". = file("D:/r_file/", "wb") # Write the column names of the data frame to the connection object. writeBin(colnames(), ) # Write the records in each of the column to the file. writeBin(c($cyl,$am,$gear), ) # Close the file for writing so that it can be read by other program. close()
Running the above file will produce a csv file and a dat binary file. This dat file stores all data as continuous bytes, so we will read it by selecting the appropriate value for the column name and column value, as follows:
#my first R program # Create a connection object to read the file in binary mode using "rb". <- file("D:/r_file/", "rb") # First read the column names. n = 3 as we have 3 columns. <- readBin(, character(), n = 3) # Next read the column values. n = 18 as we have 3 column names and 15 values. <- file("D:/r_file/", "rb") bindata <- readBin(, 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) = print(finaldata)
The above code demonstrates several output methods. If you are interested, you can expand it yourself.
This is the article about the implementation of binary file reading and writing operations in R language. For more related R languages, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!