Simplified dput() in R

后端 未结 7 1078
深忆病人
深忆病人 2020-12-02 15:24

I miss a way to add data to an SO answer in a transparent manner. My experience is that the structure object from dput() at times confuses inexperi

7条回答
  •  臣服心动
    2020-12-02 16:18

    It might be worth mentioning memCompress and memDecompress here. For in-memory objects, it can reduce the size of large objects by compressing them as specified. And the latter reverses the compression. They're actually quite useful for package objects.

    sum(nchar(dput(DF)))
    # [1] 64
    ( mDF <- memCompress(as.character(DF)) )
    # [1] 78 9c 4b d6 30 d2 51 80 20 33 1d 05 73 1d 05 0b 4d ae 64 0d 3f 47 1d 05 64 0c 14 b7 04 89 1b ea 28 18 eb 28 98 22 4b 6a 02 00 a8 ba 0c d2
    length(mDF)
    # [1] 46
    cat(mdDF <- memDecompress(mDF, "gzip", TRUE))
    # c(2, 2, 2, 6, 7, 8)
    # c(NA, NA, NA, NA, 7, 9)
    # c(1, 3, 5, NA, NA, NA)
    nchar(mdDF)
    # [1] 66
    

    I haven't quite determined if the data frame can be reassembled easily, but I'm sure it can be.

提交回复
热议问题