Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Load a Python/pandas data frame from an HDF5 file into R
#' @param h5File HDF5 file path
#' @param dataset data frame path in the HDF5 file
#' @examples
#' df <- loadhdf5data("/path/to/file.hdf5", "/path/to/dataset")
#'
loadhdf5data <- function(h5File, dataset) {
require(h5) # available on CRAN
f <- h5file(h5File)
nblocks <- h5attr(f[dataset], "nblocks")
data <- do.call(cbind, lapply(seq_len(nblocks)-1, function(i) {
data <- as.data.frame(f[paste0(dataset, "/block", i, "_values")][])
colnames(data) <- f[paste0(dataset, "/block", i, "_items")][]
data
}))
h5close(f)
data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment