Load a Python/pandas data frame from an HDF5 file into R
[This article was first published on R – Enchufa2, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The title is self-descriptive, so I will not dwell on the issue at length before showing the code. Just a small note: to my knowledge, there is only one public snippet out there that addresses this particular problem. It uses the Bioc package rhdf5
and you can find it here. The main problem is that it only works when the HDF5 file contains a single data frame, which is not very useful. This gist overcomes this limitation and uses the CRAN package h5
instead:
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 | |
} |
To leave a comment for the author, please follow the link and comment on their blog: R – Enchufa2.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.