读取存储在 HDF5 中的数据帧

标签 r hdf5

我有一个大数据框(604 个变量的 126041 个观测值)。我是 HDF5 格式的新手。我将 HDF5 文件保存如下:

writeH5DataFrame(myData,"C:/myDir/myHDF5.h5",overwrite=T)

  1. 我怎样才能读回数据框?似乎没有任何 readH5DataFrameloadH5DataFrame 功能?

  2. 此外,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/

相关文章:

r - 使用 2 个数据帧在 R 中进行有效的子集化

python - ImportError HDFStore 需要 PyTables 没有名为表的模块

Pandas :同一 HDF 中的大型数据帧?

python - pandas read_hdf 具有 'where' 条件限制?

python pandas HDF5Store 附加带有长字符串列的新数据框失败

r - 如何在数据框中返回 "TRUE"/"FALSE"匹配项?

R:匹配奇数次重复

r - 将一个非空的 data.frame 与一个空的合并时的奇怪行为

r - 按行计算给定列中的 NA

excel - 在 Excel 中加载 HDF5?