课程表

R语言 基础教程

R语言 图表

R语言 数据接口

R语言 统计示例

工具箱
速查手册

R语言 二进制文件

当前位置:免费教程 » 程序设计 » R语言

二进制文件是包含仅以位和字节(0和1)的形式存储的信息的文件。它们不是人类可读的,因为它中的字节转换为包含许多其他不可打印字符的字符和符号。尝试使用任何文本编辑器读取二进制文件将显示如Ø和ð的字符。

二进制文件必须由特定程序读取才能使用。例如,Microsoft Word程序的二进制文件只能通过Word程序读取到人类可读的形式。这表示,除了人类可读的文本之外,还有更多的信息,例如字符和页码等的格式化,它们也与字母数字字符一起存储。最后一个二进制文件是一个连续的字节序列。我们在文本文件中看到的换行符是连接第一行到下一行的字符。

有时,由其他程序生成的数据需要由R作为二进制文件处理。另外,R语言是创建可以与其他程序共享的二进制文件所必需的。

R语言有两个函数WriteBin()和readBin()来创建和读取二进制文件。

语法

  1. writeBin(object, con)
  2. readBin(con, what, n )

以下是所使用的参数的描述 -

  • con是读取或写入二进制文件的连接对象。

  • object是要写入的二进制文件。

  • 什么是模式,如字符,整数等表示要读取的字节。

  • n是从二进制文件读取的字节数。

我们考虑R语言内置数据“mtcars”。 首先,我们从它创建一个csv文件,并将其转换为二进制文件,并将其存储为操作系统文件。 接下来我们读取这个创建的二进制文件。

写入二进制文件

我们将数据帧“mtcars”读取为csv文件,然后将其作为二进制文件写入操作系统。

  1. # Read the "mtcars" data frame as a csv file and store only the columns
  2. "cyl", "am" and "gear".
  3. write.table(mtcars, file = "mtcars.csv",row.names = FALSE, na = "",
  4. col.names = TRUE, sep = ",")
  5.  
  6. # Store 5 records from the csv file as a new data frame.
  7. new.mtcars <- read.table("mtcars.csv",sep = ",",header = TRUE,nrows = 5)
  8.  
  9. # Create a connection object to write the binary file using mode "wb".
  10. write.filename = file("/web/com/binmtcars.dat", "wb")
  11.  
  12. # Write the column names of the data frame to the connection object.
  13. writeBin(colnames(new.mtcars), write.filename)
  14.  
  15. # Write the records in each of the column to the file.
  16. writeBin(c(new.mtcars$cyl,new.mtcars$am,new.mtcars$gear), write.filename)
  17.  
  18. # Close the file for writing so that it can be read by other program.
  19. close(write.filename)

读取二进制文件

上面创建的二进制文件将所有数据存储为连续字节。 因此,我们将通过选择适当的列名称值和列值来读取它。

  1. # Create a connection object to read the file in binary mode using "rb".
  2. read.filename <- file("/web/com/binmtcars.dat", "rb")
  3.  
  4. # First read the column names. n = 3 as we have 3 columns.
  5. column.names <- readBin(read.filename, character(), n = 3)
  6.  
  7. # Next read the column values. n = 18 as we have 3 column names and 15 values.
  8. read.filename <- file("/web/com/binmtcars.dat", "rb")
  9. bindata <- readBin(read.filename, integer(), n = 18)
  10.  
  11. # Print the data.
  12. print(bindata)
  13.  
  14. # Read the values from 4th byte to 8th byte which represents "cyl".
  15. cyldata = bindata[4:8]
  16. print(cyldata)
  17.  
  18. # Read the values form 9th byte to 13th byte which represents "am".
  19. amdata = bindata[9:13]
  20. print(amdata)
  21.  
  22. # Read the values form 9th byte to 13th byte which represents "gear".
  23. geardata = bindata[14:18]
  24. print(geardata)
  25.  
  26. # Combine all the read values to a dat frame.
  27. finaldata = cbind(cyldata, amdata, geardata)
  28. colnames(finaldata) = column.names
  29. print(finaldata)

当我们执行上面的代码,它产生以下结果和图表 -

  1. [1] 7108963 1728081249 7496037 6 6 4
  2. [7] 6 8 1 1 1 0
  3. [13] 0 4 4 4 3 3
  4.  
  5. [1] 6 6 4 6 8
  6.  
  7. [1] 1 1 1 0 0
  8.  
  9. [1] 4 4 4 3 3
  10.  
  11. cyl am gear
  12. [1,] 6 1 4
  13. [2,] 6 1 4
  14. [3,] 4 1 4
  15. [4,] 6 0 3
  16. [5,] 8 0 3

正如我们所看到的,我们通过读取R中的二进制文件得到原始数据。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号