D. Studer
D. Studer

Reputation: 1875

stack different dataframes in a function

I am having troubles to stack a variable number of data-frames whithin a for-loop. Can someone please help me?

# Load libraries
library(dplyr)
library(tidyverse)
library(here)

A function that should open all excel-files and create one single file using plyr::rbind.fill():

stackDfs <- function(filenames){
  for(fn in filenames){
    df1 <- openxlsx::read.xlsx(here::here("folder1", "folder2", fn), sheet=6, detectDates = TRUE)
    # ... do some additional mutations here
  }
    
  all_dfs <- (plyr::rbind.fill(df1, df2, df3, df4, ...)
  return(all_dfs)
}

Here I am defining which files should be opened and call the stacking-function. The number of files to be stacked should be variable.

filenames <- c("filexy-20210202.xlsx", "filexy-2021-20210205.xlsx")
stackDfs(filenames)

Upvotes: 1

Views: 44

Answers (2)

Abigail
Abigail

Reputation: 470

In line with the comment posted about using purrr, if you put all the files in the same folder you can use a package called fs instead of manually entering all the file names:

library(tidyverse)
library(here)
library(fs)

filenames <- fs::dir_ls(here("files"))

all_dfs <- filenames %>%
   map_dfr(openxlsx:read.xlsx, sheet = 6, detectDates = TRUE)
all_dfs

Upvotes: 1

akrun
akrun

Reputation: 887231

With the current setup, we can initialize a list, read the data into the list and then apply rbind.fill within do.call

stackDfs <- function(filenames){
    out_lst <- vector('list', length(filenames))
    names(out_list) <- filenames
    for(fn in filenames){
     out_list[[fn]] <- openxlsx::read.xlsx(here::here("folder1",
          "folder2", fn), sheet=6, detectDates = TRUE)
      #....
     }
   all_dfs <- do.call(plyr::rbind.fill, out_lst)
   all_dfs
}

Upvotes: 2

Related Questions