W/R .bin in R

tData = as.integer(2^(1:18))

# make sure what you are writing is the same format (e.g. integer) as reading.
pFileWrite = file('test_data/test.bin', 'wb')
writeBin(tData, pFileWrite, size=4, endian='little')
close(pFileWrite)

pFileRead = file('test_data/test.bin', 'rb')
t = readBin(pFileRead, n=18, what='integer', size=4, endian='little')
close(pFileRead)

# or use file.size():
file_input = 'test_data/test.bin'
pFileRead = file(file_input, 'rb')
t = readBin(pFileRead, n=file.size(file_input)/4, integer(), size=4, endian='little')
close(pFileRead)

For float32, use "numeric":

tData = as.vector(as.single(c(1.1, 2.2, -3.45)))

pFileWrite = file('data.bak/test.bin', 'wb')
writeBin(tData, pFileWrite, size=4, endian='little')
close(pFileWrite)

pFileRead = file('data.bak/test.bin', 'rb')
t = readBin(pFileRead, n=18, what='numeric', size=4, endian='little')
close(pFileRead)

For float64, use "size=8":

tData = as.vector(c(1.1, 2.2, -3.45))

pFileWrite = file('data.bak/test.bin', 'wb')
writeBin(tData, pFileWrite, size=8, endian='little')
close(pFileWrite)

pFileRead = file('data.bak/test.bin', 'rb')
t = readBin(pFileRead, n=18, what='numeric', size=8, endian='little')
close(pFileRead)

It seems that "as.single" is not necessary, we can use "size" to control 32/64bits.

W/R .mat in R

install.packages('R.matlab')
library('R.matlab')
# write
writeMat('fileName.mat', 
         variableNameInMat01 = variable.1.in.r,
         variableNameInMat02 = variable.2.in.r
         )

# read: Returns a named "list" structure containing all variables
data = readMat('fileName.mat')
str(data)

ref

cite: Henrik Bengtsson (2016). R.matlab: Read and Write MAT Files and Call MATLAB from
Within R. R package version 3.6.0-9000. https://github.com/HenrikBengtsson/R.matlab

W/R .npy in R

install.packages('RcppCNPy')
library(RcppCNPy)
variableFoo = npyLoad('fileName.npy') # read crap, NOT working !!!
variableFoo

ref

W/R .csv in R

# write:
write.table(MyDataDf, file='MyData.csv', sep=',', row.names=FALSE)
write.table(MyDataDf, file='MyData.csv', sep=',', row.names=FALSE, na='', col.names=FALSE)
#write.csv(MyDataDf, file="MyData.csv", sep=" ", row.names=FALSE) # do NOT use !!! sep is space and can NOT be changed.

# read:
read.csv('MyData.csv') # WARN !!! OBS: strings/chars are interpreted as levels (internally int).

W/R .bin in Numpy

import numpy as np

data = np.array([1.1, 2.2, -3.14]) # default, float64
data

data.tofile('test.bin') # default, float64
# or:
data.astype(np.float32).tofile('test.bin') # convert to float32

data = np.fromfile('data.bak/test.bin', '<f8')  # little-endian float64
data

Meaning of dtype = '<f8':
<: little-endian, win-tel default.
f: float (default); i for int.
8: 4/8 bytes == 32/64 bits.
Endian and 32/64 bits default values depend on platforms.
ref: numpy.ndarray.tofile(), numpy.fromfile()

part of .bin file

My combined instructions in GitHub.
Separated instructions below:
read part of a file by seek()

item_size_bytes = 4 # 4: 32bits, 8: 64bit
seek_nr = the_number_of_floats_or_integeters_to_skip * item_size_bytes
item_nr = the_number_of_floats_or_integeters_to_read
file_read = '/dev/shm/ttt.bin'
p_file = open(file_read, "rb")
p_file.seek(seek_nr, os.SEEK_SET)
t = np.fromfile(p_file, dtype=np.float32, count=item_nr)
t
p_file.close() # optional ???

ref

append to a .bin file: ref

W/R .npy in NumPy

or .npz for compressed

# binary
np.save('var_name.npy', var_name)
t = np.load('var_name.npy')

# txt
np.savetxt('var_name.txt', var_name, fmt='%d')
t = np.loadtxt('var_name.txt', dtype=int)

ref

W/R .pkl in Pandas

//data_frame

# write:
my_data_df.to_pickle('my_data_df.pkl')
# read:
my_data_df = pd.read_pickle('my_data_df.pkl')

W/R .rdata in Py

Write:

from rpy2.robjects import r, pandas2ri
pandas2ri.activate() # important !!!

import numpy as np
import pandas as pd

# create
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C':[7,8,9]}, index=['one', 'two', 'three'])

# convert
r_dataframe = pandas2ri.py2ri(df)
print(type(r_dataframe))
print(r_dataframe)

# save
r.assign(r_variable_name_string, r_dataframe)
r("save(" + r_variable_name_string + ", file='foo.bar.RData', compress=TRUE)")

// see solo-cuda: http://localhost:8889/notebooks/impute-5.2-newAPI-convert-to-r.ipynb

Read:
TODO.

W/R .mat in Py

// see solo-cuda: http://localhost:8889/notebooks/impute-5-convert-to-r.ipynb
TODO.
Suggest: use .csv as a middle file.

W/R HDFS in Pandas

store = HDFStore('store.h5')

store['df'] = df  # save it
store['df']  # load it

ref

W/R .csv in Pandas

# write:
df.to_csv('my_data_df.csv', index=False) # do NOT save row indexes
df.to_csv('my_data_df.csv', index=False, sep='\t', encoding='utf-8')

# read:
pd.read_csv('my_data_df.csv') # sep is comma by default

ref overflow
ref doc

W/R .bin in Matlab

'b' argument of 'fopen' means big-endian.