1. Save/read binary files | R Documentation |
Save/read a numeric data as a fortran-formatted binary file at a defined precision (single or double).
saveBinary(X, file = paste0(tempdir(), "/file.bin"),
precision.format = c("double","single"),
verbose = TRUE)
readBinary(file = paste0(tempdir(), "/file.bin"),
rows = NULL, cols = NULL,
drop = TRUE, verbose = TRUE)
X |
(numeric matrix) Data to save |
file |
(character) Name of the binary file to save/read |
precision.format |
(character) Either 'single' or 'double' for numeric precision and memory occupancy (4 bytes/32-bit or 8 bytes/64-bit, respectively) of the matrix to save |
rows |
(integer vector) Which rows are to be read from the file. Default |
cols |
(integer vector) Which columns are to be read from the file. Default |
drop |
Either |
verbose |
|
Function 'saveBinary' does not return any value but print a description of the file saved.
Function 'readBinary' returns the data that was read.
require(SFSI)
# A numeric matrix
X = matrix(rnorm(500*100), ncol=100)
# Save matrix as double-precision
filename1 = paste0(tempdir(),"/Matrix1.bin")
saveBinary(X, filename1, precision.format="double")
# Save matrix as single-precision
filename2 = paste0(tempdir(),"/Matrix2.bin")
saveBinary(X, filename2, precision.format="single")
# Read the double-precision matrix
X2 = readBinary(filename1)
max(abs(X-X2)) # No loss of precision
file.info(filename1)$size # Size of the file
# Read the single-precision matrix
X3 = readBinary(filename2)
max(abs(X-X3)) # Loss of precision
file.info(filename2)$size # But smaller-size file
# Read specific rows and columns
rows = c(2,4,5,8,10)
cols = c(1,2,5)
(X2 = readBinary(filename1, rows=rows, cols=cols))
# Equal to:
X[rows,cols]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.