R-binary-files

提供:Dev Guides
移動先:案内検索

R-バイナリファイル

バイナリファイルは、ビットとバイトの形式でのみ格納された情報を含むファイルです(0と1)。 その中のバイトは、他の多くの印刷不可能な文字を含む文字や記号に変換されるため、人間が読むことはできません。 テキストエディターを使用してバイナリファイルを読み取ろうとすると、Øやðなどの文字が表示されます。

バイナリファイルを使用するには、特定のプログラムで読み取る必要があります。 たとえば、Microsoft Wordプログラムのバイナリファイルは、Wordプログラムでのみ人間が読み取れる形式に読み取ることができます。 これは、人間が読めるテキストの他に、文字やページ番号などの書式設定など、英数字とともに保存される情報がはるかに多いことを示しています。 最後に、バイナリファイルは連続したバイトシーケンスです。 テキストファイルに表示される改行は、最初の行を次の行に結合する文字です。

他のプログラムによって生成されたデータは、Rによってバイナリファイルとして処理される必要がある場合があります。 また、Rは他のプログラムと共有できるバイナリファイルを作成するために必要です。

Rには、バイナリファイルを作成および読み取るための2つの関数* WriteBin()および readBin()*があります。

構文

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

以下は、使用されるパラメータの説明です-

  • con は、バイナリファイルを読み書きする接続オブジェクトです。
  • object は書き込まれるバイナリファイルです。
  • what は、文字、整数などのモードです。 読み込むバイトを表します。
  • n は、バイナリファイルから読み取るバイト数です。

Rの組み込みデータ「mtcars」を考慮します。 まず、csvファイルを作成してバイナリファイルに変換し、OSファイルとして保存します。 次に、Rに作成されたこのバイナリファイルを読み取ります。

バイナリファイルの作成

データフレーム「mtcars」をcsvファイルとして読み取り、バイナリファイルとして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)

バイナリファイルの読み取り

上記で作成されたバイナリファイルは、すべてのデータを連続したバイトとして保存します。 したがって、列の値と同様に列名の適切な値を選択して読み取ります。

# 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)

上記のコードを実行すると、次の結果とグラフが生成されます-

 [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

ご覧のとおり、Rのバイナリファイルを読み取ることで元のデータを取得しました。