什么决定了R中保存对象的大小?
使用save()从R保存对象时,什么决定了保存文件的大小?显然,它与object.size()确定的对象大小不同(或接近) 例如: 我读取数据帧并使用什么决定了R中保存对象的大小?,r,memory,workspace,R,Memory,Workspace,使用save()从R保存对象时,什么决定了保存文件的大小?显然,它与object.size()确定的对象大小不同(或接近) 例如: 我读取数据帧并使用 snpmat=read.table("Heart.txt.gz",header=T) save(snpmat,file="datamat.RData") 文件datamat.RData的大小为360MB > object.size(snpmat) 4998850664 bytes #Much larger 然后,我进行了一
snpmat=read.table("Heart.txt.gz",header=T)
save(snpmat,file="datamat.RData")
文件datamat.RData的大小为360MB
> object.size(snpmat)
4998850664 bytes #Much larger
然后,我进行了一些回归分析,得到了另一个相同维度的数据框adj.snpmat(6820000行80列)
我使用
> save(adj.snpmat,file="adj.datamat.RData")
现在文件adj.datamat.RData的大小是3.3GB。我不明白为什么这两个文件的大小如此不同,而object.size()给出了相似的大小。欢迎了解决定保存对象大小的因素
更多信息:
> typeof(snpmat)
[1] "list"
> class(snpmat)
[1] "data.frame"
> typeof(snpmat[,1])
[1] "integer"
> typeof(snpmat[,2])
[1] "double" #This is true for all columns except column 1
> typeof(adj.snpmat)
[1] "list"
> class(adj.snpmat)
[1] "data.frame"
> typeof(adj.snpmat[,1])
[1] "character"
> typeof(adj.snpmat[,2])
[1] "double" #This is true for all columns except column 1
您的矩阵非常不同,因此压缩方式也非常不同 SNP数据只包含几个值(例如,1或0),而且非常稀疏。这意味着它非常容易压缩。例如,如果有一个全零的矩阵,可以考虑通过指定单个值(0)和维度来压缩数据
您的回归矩阵包含许多不同类型的值,并且也是实数(我假设p值、系数等),这使得它的可压缩性大大降低 保存的
.Rdata
文件被压缩,因此应该更小。小多少取决于压缩算法和您的数据。在我的示例中,原因可能是什么?换句话说,你能告诉我数据的哪些方面会影响它吗?@Pratyay你对压缩了解多少?读一读可能会有好处,因为这基本上就是你要问的@戴森,谢谢。我浏览了链接。但我仍然不明白它在R中是如何具体工作的。哪些数据类型压缩得更好?另外,在我的示例中,这两个数据帧具有所有相似之处。为什么在压缩方面有如此巨大的差异?另外,在保存对象时是否有生成较小文件的技巧?例如,使用某些特定的数据类型或其他类型?我有一个相同的问题,即object.size
的5Mb左右的变量在保存后变为750Mb!
> typeof(snpmat)
[1] "list"
> class(snpmat)
[1] "data.frame"
> typeof(snpmat[,1])
[1] "integer"
> typeof(snpmat[,2])
[1] "double" #This is true for all columns except column 1
> typeof(adj.snpmat)
[1] "list"
> class(adj.snpmat)
[1] "data.frame"
> typeof(adj.snpmat[,1])
[1] "character"
> typeof(adj.snpmat[,2])
[1] "double" #This is true for all columns except column 1