How to write a binary file with 4 bytes per entry using R?

Hi, y'all

I'm trying to write a data frame into a binary file. I know how to do it using writeBin, but I don't know how to specify the format that the data are to be stored.

This output binary file is to be read by a FORTRAN application, and the data it expects should be read as a sequence of 320 number per row, each number a 4-byte integer.

I happen to know that in FORTRAN, for instance, you could specify such format as: 320(I4), where I4 stands for 4-byte integer, but I am uncertain about doing it in R. I tried the following but I received an error message back.

# Fake data
df = matrix(data = rnorm(320*100, mean = 100), ncol = 320, nrow = 100)
# Set up a temppfile
xfil = tempfile("testbin")
x = file(xfil, "wb")
# Write your data to a binary file
writeBin(object = df,
         con = x,
         size = 320*4)
#> Error in writeBin(object = df, con = x, size = 320 * 4): can only write vector objects

Could anyone help me out?
Thank you.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.