{"id":1731,"date":"2020-12-04T20:32:18","date_gmt":"2020-12-04T20:32:18","guid":{"rendered":"https:\/\/ded9.com\/?p=1731"},"modified":"2025-12-20T11:41:45","modified_gmt":"2025-12-20T11:41:45","slug":"learning-binary-files-in-r-programming-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/learning-binary-files-in-r-programming-language\/","title":{"rendered":"Learning Binary Files in R Programming Language: Guide for Data I\/O"},"content":{"rendered":"<p><span style=\"font-size: 12pt; font-family: georgia, palatino, serif;\">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.\u00a0<\/span><\/p>\n<p>Attempt to read binary Files using any text editor; they may contain characters such as \u00d8.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Finally, a binary File is a continuous sequence of bytes. The breakline in a text File is a character that indicates the end of a line.<\/p>\n<p>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.<\/p>\n<p>In the <a href=\"https:\/\/ded9.com\/basic-programming-syntax-training-r\/\">R programming language<\/a>, there are two functions for creating and reading binary files: writeBin() and readBin(), respectively.<\/p>\n<h2><span style=\"font-size: 18pt;\">Syntax<\/span><\/h2>\n<blockquote>\n<p dir=\"ltr\">writeBin (object, con)<\/p>\n<p dir=\"ltr\">readBin (con, what, n)<\/p>\n<\/blockquote>\n<p>Parameters used in the above code are as follows:<\/p>\n<ul>\n<li><strong>Con<\/strong>\u00a0is the connection object for reading and writing binary files.<\/li>\n<li><strong>Object is a<\/strong> binary File that must be written.<\/li>\n<li><strong>What are states, such as characters, integers, and so on?<\/strong>\u00a0Indicates bytes to be read.<\/li>\n<li><strong>N is the<\/strong> number of bytes to be read from the binary File.<\/li>\n<\/ul>\n<p><strong>Example<\/strong><\/p>\n<p>We first consider the &#8220;mtcars&#8221; dataset, which is an internal R dataset. We can create a CSV File from it, convert it to a binary File, and save it as an OS File. Then we read the binary file created in R.<\/p>\n<h2>Write a Binary File<\/h2>\n<h2><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258635 size-full\" title=\"Binary Files\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/unnamed.jpg\" alt=\"Write a Binary File\" width=\"512\" height=\"384\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/unnamed.jpg 512w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/unnamed-300x225.jpg 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/h2>\n<p>We read the &#8220;mtcars&#8221; data frame as a CSV File and then write it as a binary File for the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Operating_system\" target=\"_blank\" rel=\"noopener\">operating System<\/a>.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Read the \u201cmtcars\u201d data frame as a csv file and store only the columns\r\n\r\n\"Cyl\", \"am\" and \"gear\".\r\n\r\nwrite.table (mtcars, file = \u201cmtcars.csv\u201d, row.names = FALSE, na = \u201c\u201d,\r\n\r\ncol.names = TRUE, sep = \u201c,\u201d)\r\n\r\n# Store 5 records from the csv file as a new data frame.\r\n\r\nnew.mtcars &lt;- read.table (\u201cmtcars.csv\u201d, sep = \u201c,\u201d, header = TRUE, nrows = 5)\r\n\r\n# Create a connection object to write the binary file using \u201cwb\u201d mode.\r\n\r\nwrite.filename = file (\"\/ web \/ com \/ binmtcars.dat\", \"wb\")\r\n\r\n# Write the column names of the data frame to the connection object.\r\n\r\nwriteBin (colnames (new.mtcars), write.filename)\r\n\r\n# Write the records in each of the column to the file.\r\n\r\nwriteBin (c (new.mtcars $ cyl, new.mtcars $ am, new.mtcars $ gear), write.filename)\r\n\r\n# Close the file for writing so that it can be read by other program.\r\n\r\nclose (write.filename)<\/pre>\n<\/div>\n<h2>Read Binary Files<\/h2>\n<p>The binary created above stores all data as a sequence of bytes. We read that File by selecting the correct name and values \u200b\u200bfor the columns.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Create a connection object to read the file in binary mode using \u201crb\u201d.\r\n\r\nread.filename &lt;- file (\"\/ web \/ com \/ binmtcars.dat\", \"rb\")\r\n\r\n# First read the column names. n = 3 as we have 3 columns.\r\n\r\ncolumn.names &lt;- readBin (read.filename, character (), n = 3)\r\n\r\n# Next read the column values. n = 18 as we have 3 column names and 15 values.\r\n\r\nread.filename &lt;- file (\"\/ web \/ com \/ binmtcars.dat\", \"rb\")\r\n\r\nbindata &lt;- readBin (read.filename, integer (), n = 18)\r\n\r\n# Print the data.\r\n\r\nprint (bindata)\r\n\r\n# Read the values \u200b\u200bfrom 4th byte to 8th byte which represents \u201ccyl\u201d.\r\n\r\ncyldata = bindata [4: 8]\r\n\r\nprint (cyldata)\r\n\r\n# Read the values \u200b\u200bform 9th byte to 13th byte which represents \u201cam\u201d.\r\n\r\namdata = bindata [9:13]\r\n\r\nprint (amdata)\r\n\r\n# Read the values \u200b\u200bform 9th byte to 13th byte which represents \u201cgear\u201d.\r\n\r\ngeardata = bindata [14:18]\r\n\r\nprint (geardata)\r\n\r\n# Combine all the read values \u200b\u200bto a dat frame.\r\n\r\nfinaldata = cbind (cyldata, amdata, geardata)\r\n\r\ncolnames (finaldata) = column.names\r\n\r\nprint (finaldata)<\/pre>\n<\/div>\n<p>When we run the above code, the following results and diagrams are executable:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">[1] 7108996 1728081249 7496037 6 6 4\r\n\r\n[7] 6 8 1 1 1 0\r\n\r\n[13] 0 4 4 4 3 3\r\n\r\n[1] 6 6 4 6 8\r\n\r\n[1] 1 1 1 0 0\r\n\r\n[1] 4 4 4 3 3\r\n\r\ncyl am gear\r\n\r\n[1,] 6 1 4\r\n\r\n[2,] 6 1 4\r\n\r\n[3,] 4 1 4\r\n\r\n[4,] 6 0 3\r\n\r\n[5,] 8 0 3<\/pre>\n<\/div>\n<p>As you can see, the primary data is obtained by reading the binary File in R.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What are binary files in R?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Binary files store data in non-text format allowing more compact, faster input\/output compared to plain text.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you read a binary file in R?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use functions like file() with \"rb\" mode and readBin() to open and read binary data.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you write binary data in R?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Open a connection with file() in \"wb\" mode and use writeBin() to save binary data.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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.\u00a0 Attempt to read binary Files using any text editor; they may contain characters [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":1732,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[174],"tags":[3324],"class_list":["post-1731","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-r-studio","tag-r"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=1731"}],"version-history":[{"count":6,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1731\/revisions"}],"predecessor-version":[{"id":266346,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1731\/revisions\/266346"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/1732"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=1731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=1731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=1731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}