Skip to content

Instantly share code, notes, and snippets.

@Enchufa2
Created May 9, 2017 15:52
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Enchufa2/3cca2cb0f625139fabc505bca6c1dc85 to your computer and use it in GitHub Desktop.
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