在R中写入二进制文件

发布于 2024-11-28 16:49:14 字数 191 浏览 1 评论 0原文

我被要求将 R 输出写入两个二进制文件,一个索引文件和一个主数据文件。索引文件中的每个id都会对应一个矩阵/块。我在互联网上读到过有关在 R 中编写二进制文件的信息,但我不确定如何指定格式以便我可以实现这种格式?

另外,我们可以在 R 中指定短整数吗?他说他希望数字是短整数(两个字节),但我不知道这意味着什么。

我很感激任何意见!谢谢

I am asked to write R output in two binary files, an index file and a main data file. There will be one matrix/block corresponding to each id in the index file. I have read about writing binary files in R on the internet but I am not sure how to specify the format so that I can achieve this format?

Also, can we specify short integer in R? He said he wants the numebers to be short intergets (two bytes) and I don't want what that means.

I appreciate any input! Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2024-12-05 16:49:14

由于你没有把问题说得很清楚,所以我在下面的示例代码中做了一些假设。给定一个矩阵列表,它将它们保存到 .bin 文件中,并创建一个带有偏移量的 .idx 文件。然后,您可以根据给定的索引再次将它们加载回来。未使用您提到的 2 字节大小 - 它将矩阵数据保存为 8 字节双精度数或 4 字节整数(但您可以更改它)。

它的使用方法如下:

mtx <- list(matrix(1:12,4), matrix(sin(1:12),4))
saveMatrixList("c:/foo", mtx)

loadMatrix("c:/foo", 1)
loadMatrix("c:/foo", 2)

...以下是其功能:

saveMatrixList <- function(baseName, mtxList) {
    idxName <- paste(baseName, ".idx", sep="")
    idxCon <- file(idxName, 'wb')
    on.exit(close(idxCon))

    dataName <- paste(baseName, ".bin", sep="")
    con <- file(dataName, 'wb')
    on.exit(close(con))

    writeBin(0L, idxCon)

    for (m in mtxList) {
        writeBin(dim(m), con)
        writeBin(typeof(m), con)
        writeBin(c(m), con) 
        flush(con)

        offset <- as.integer(seek(con))
        cat('offset', offset)
        writeBin(offset, idxCon)
    }

    flush(idxCon)
}

loadMatrix <- function(baseName = "data", index) {
    idxName <- paste(baseName, ".idx", sep="")
    idxCon <- file(idxName, 'rb')
    on.exit(close(idxCon))

    dataName <- paste(baseName, ".bin", sep="")
    con <- file(dataName, 'rb')
    on.exit(close(con))

    seek(idxCon, (index-1)*4)
    offset <- readBin(idxCon, 'integer')

    seek(con, offset)
    d <- readBin(con, 'integer', 2)
    type <- readBin(con, 'character', 1)
    structure(readBin(con, type, prod(d)), dim=d)
}

Since you didn't specify the problem very clearly, I made some assumptions in the sample code below. Given a list of matrices, it saves them to a .bin file and creates an .idx file with offsets. You can then load them back in again given an index. The 2-byte size you mentioned isn't used - it saves the matrix data as 8-byte doubles or 4-byte integers (but you could change that).

Here's how it's used:

mtx <- list(matrix(1:12,4), matrix(sin(1:12),4))
saveMatrixList("c:/foo", mtx)

loadMatrix("c:/foo", 1)
loadMatrix("c:/foo", 2)

...and here are the functions:

saveMatrixList <- function(baseName, mtxList) {
    idxName <- paste(baseName, ".idx", sep="")
    idxCon <- file(idxName, 'wb')
    on.exit(close(idxCon))

    dataName <- paste(baseName, ".bin", sep="")
    con <- file(dataName, 'wb')
    on.exit(close(con))

    writeBin(0L, idxCon)

    for (m in mtxList) {
        writeBin(dim(m), con)
        writeBin(typeof(m), con)
        writeBin(c(m), con) 
        flush(con)

        offset <- as.integer(seek(con))
        cat('offset', offset)
        writeBin(offset, idxCon)
    }

    flush(idxCon)
}

loadMatrix <- function(baseName = "data", index) {
    idxName <- paste(baseName, ".idx", sep="")
    idxCon <- file(idxName, 'rb')
    on.exit(close(idxCon))

    dataName <- paste(baseName, ".bin", sep="")
    con <- file(dataName, 'rb')
    on.exit(close(con))

    seek(idxCon, (index-1)*4)
    offset <- readBin(idxCon, 'integer')

    seek(con, offset)
    d <- readBin(con, 'integer', 2)
    type <- readBin(con, 'character', 1)
    structure(readBin(con, type, prod(d)), dim=d)
}
飘落散花 2024-12-05 16:49:14

请参阅help(writeBin),size = 2 定义对每个元素的分配(即两个字节整数)。但如果您不知道这意味着什么,您可能需要请求者提供更多信息。

See help(writeBin), size = 2 defines the allocation to each element (i.e. a two byte integer). But if you don't know what this means you probably will need a lot more information from your requester.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文