我有一个大数据框(604 个变量的 126041 个观测值)。我是 HDF5 格式的新手。我将 HDF5 文件保存如下:
writeH5DataFrame(myData,"C:/myDir/myHDF5.h5",overwrite=T)
我怎样才能读回数据框?似乎没有任何
readH5DataFrame
或loadH5DataFrame
功能?此外,
writeH5DataFrame
花费的时间非常长,可能是因为列数很多(在本例中为 604)。文档提到“每一列的数据都存储在一个单独的 H5Dataset 中。”——不确定这是否是花费很长时间的原因。有什么方法可以加快编写 HDF5 格式的 DataFrame 的速度吗?
最佳答案
我不知道你用的是哪个包,但是使用rhdf5
包,写/读hdf5文件看起来很容易。
## uncomment the 2 lines after to install the package
## source("http://bioconductor.org/biocLite.R")
## biocLite("rhdf5")
library(rhdf5)
## empty HDF5 file : the data base
h5createFile("myhdf5file.h5")
## create group hierarchy. : tables or datasets
h5createGroup("myhdf5file.h5","group1")
h5createGroup("myhdf5file.h5","group2")
## save a matrix
A = matrix(1:10,nr=5,nc=2)
h5write(A, "myhdf5file.h5","group1/A")
## save an array with attribute
B = array(seq(0.1,2.0,by=0.1),dim=c(5,2,2))
attr(B, "scale") <- "liter"
h5write(B, "myhdf5file.h5","group2/B")
## check the data base
h5ls("myhdf5file.h5")
group name otype dclass dim
0 / group1 H5I_GROUP
1 /group1 A H5I_DATASET INTEGER 5 x 2
2 / group2 H5I_GROUP
3 /group2 B H5I_DATASET FLOAT 5 x 2 x 2
## read A and B
D = h5read("myhdf5file.h5","group1/A")
E = h5read("myhdf5file.h5","group2/B")
关于读取存储在 HDF5 中的数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20928291/