sarah99
sarah99

Reputation: 23

Create loop to change structure of multiple data frames in R

I have a bunch of excel files that I have loaded into R as separate dataframes. I now need to change the structure/layout of every one of these data frames. I have done all of this separately, but it is becoming very time consuming. I am not sure how there is a better way to accomplish this. My guess would be that I need to combine them all into a list and then create some type of loop to go through every data frame in that list. I need to be able to remove rows and columns from the edge, add 'row' the top left cell that is currently empty, and then follow that pivot_longer, mutate, and select functions that I have listed below that I have done separately.

    names(df)[1] <- 'row'
    df <- df %>%
       pivot_longer((!row), names_to = "plateColumn", values_to = "Broth_t0")
    df <- df %>%
       mutate(wellID = paste0(row, plateColumn)) %>%
       select(-c(row, plateColumn))

I have tried what is below and I get an error, does anyone have a better way that what I am currently doing to accomplish this?

   for(x in seq_along(files.list)){
     names(files.list)[1] <- 'row'
     df <- df %>%
        pivot_longer((!row), names_to = "plateColumn", values_to = "Broth_t0")
     df <- df %>%
        mutate(wellID = paste0(row, plateColumn)) %>%
        select(-c(row, plateColumn))
      }

Upvotes: 0

Views: 146

Answers (1)

Arthur
Arthur

Reputation: 2402

If you have a vector of filenames my_files, I think this will work

library(tidyverse)
library(readxl)

prepare_df <- function(df) {
  # make changes to df
  names(df)[1] <- 'row'
  df <- df %>%
    pivot_longer((!row), names_to = "plateColumn", values_to = "Broth_t0")
  df <- df %>%
    mutate(wellID = paste0(row, plateColumn)) %>%
    select(-c(row, plateColumn))
  return(df)
}

names(my_files) <- my_files          # often useful if the vector we're mapping over has names
dfs <- map(my_files, read_excel)     # read into a list of data frames
dfs <- map(dfs, prepare_df)          # prepare each one
df  <- bind_rows(dfs, .id = "file")  # if you prefer one data frame instead

Upvotes: 1

Related Questions