Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

BinaryFiles

Binary files


Description

Save/read a numeric data as a fortran-formatted binary file at a defined precision (single or double).

Usage

saveBinary(X, file = paste0(tempdir(), "/file.bin"), 
 row.names = TRUE, col.names = TRUE, 
 type = c("float","double"), verbose = TRUE)
  
readBinary(file = paste0(tempdir(), "/file.bin"), 
  indexRow = NULL, indexCol = NULL, verbose = TRUE)

Arguments

X

(numeric matrix) Data to save

file

(character) Name of the binary file to save/read

row.names

TRUE or FALSE to whether save rows' names

col.names

TRUE or FALSE to whether save columns' names

type

(character) Either 'float' or 'double' for single (4 bytes) or double precision (8 bytes), respectively, that matrix to save will occupy

indexRow

(integer vector) Which rows are to be read from the file. Default indexRow=NULL will read all the rows

indexCol

(integer vector) Which columns are to be read from the file. Default indexCol=NULL will read all the columns

verbose

TRUE or FALSE to whether printing file information

Value

Function 'saveBinary' does not return any value but print a description of the file saved.

Function 'readBinary' returns the data that was read.

Author(s)

Marco Lopez-Cruz (maraloc@gmail.com) and Gustavo de los Campos

Examples

require(SFSI)

  # Simulate matrix
  n = 1000; m = 5
  X = matrix(rnorm(n*m),ncol=m)
  head(X)
  
  # Save matrix
  saveBinary(X,paste0(tempdir(),"/Matrix1.bin"),type="double") # as double-precision
  saveBinary(X,paste0(tempdir(),"/Matrix2.bin"),type="float")  # as single-precision

  # Read the double-precision matrix
  X2 = readBinary(paste0(tempdir(),"/Matrix1.bin"))
  head(X2)
  max(abs(X-X2))     # No loss of precision
  object.size(X2)    # Size of the object

  # Read the single-precision matrix
  X3 = readBinary(paste0(tempdir(),"/Matrix2.bin"))
  head(X3)
  max(abs(X-X3))     # Loss of precision
  object.size(X3)    # But smaller-size object

  # Read specific rows and columns
  indexRow = c(2,4,5,8,10)
  indexCol = c(1,2,5)
  X2 = readBinary(paste0(tempdir(),"/Matrix1.bin"),indexRow=indexRow,indexCol=indexCol)
  X2
  # Equal to: 
  X[indexRow,indexCol]

SFSI

Sparse Family and Selection Index

v0.3.0
GPL-3
Authors
Marco Lopez-Cruz [aut, cre], Gustavo de los Campos [aut], Paulino Perez-Rodriguez [ctb]
Initial release
2021-04-29

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.