Our
Our

Reputation: 1035

Combine multiple datasets in a more compact way

Whenever I have several datasets that I want to load and combine into a single dataset, so that I can analyse all of them at the same time, I do something like

for(i in 1:length(dataset_paths))
{
  data_path <- list.files(path = paste0(here(dataset_paths[i]), "results/analysis/"), pattern = ".*\\.degradation_dynamics\\.csv", full.names = TRUE)
  t_dt <- fread(data_path)
  if(i == 1)
  {
    dt <- t_dt
  }
  dt <- rbind(dt, t_dt)
}
rm(t_dt)
dt %>% setkey(frame)

This code is quite ugly.

Is there a way to make this code more smaller & more understandable?

For instance by

Upvotes: 0

Views: 222

Answers (2)

undercover_camel
undercover_camel

Reputation: 38

I like foreach for this purpose:

require(data.table)
require(foreach)

dt <- foreach( path = dataset_paths, .combine='rbind' ) %do% {
  
  dt_t <- fread(path)
  
}

setkey(dt,frame)

Upvotes: 1

stefan
stefan

Reputation: 123818

Be aware that the following code is not tested, but using lapply you do something like:

dt <- lapply(dataset_paths, function(x) {
  data_path <- list.files(path = here(x, "results", "analysis"), pattern = ".*\\.degradation_dynamics\\.csv", full.names = TRUE)
  fread(data_path)
})
dt <- do.call(rbind, dt)
dt %>% setkey(frame)

Upvotes: 1

Related Questions