简体   繁体   中英

Is there a way to save a dataframes from an R package as an hdf5 to load into python?

I'm trying to export each dataset from the CASdatasets package ( http://cas.uqam.ca/ ) loop through each and save them to an hdf5 file for each to load into python as pandas dataframes. However, I'm not as familiar with R as python. Is there a way to loop through each dataset in the package and save each to a disk as an hdf5 file?

so far, I have

install.packages("CASdatasets", repos = "http://dutangc.free.fr/pub/RRepos/", type="source")
library(CASdatasets)

There are different ways to do so. This one here loads the rda files directly from the library into a fresh environment. From there, you can export to any file format.

library(feather)

all_data <- list.files(
  file.path(.libPaths()[1], "CASdatasets", "data"), 
  pattern = "\\.rda", 
  full.names = TRUE
)

# Load all files into a fresh environment
data_env <- new.env()
lapply(all_data, load, envir = data_env)

# Export into your favourite format
for (f in names(data_env)[1:3]) {
  if (is.data.frame(f))
    write_feather(data_env[[f]], paste(f, ".feather"))
  else
    warning(f)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM