Load a Python/pandas data frame from an HDF5 file into R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' @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