Read in multiple .csv files, transform them and save as new files in R

140 Views Asked by At

I have 90 .csv files in my working directory and each of them is a 90*90 matrix. The files don't have consistent names (here is a sample)

>list.files()
[1] "sub-001-SGU-cn.csv" 
[2] "sub-001-SGU-nv.csv" 
[3] "sub-002-Cam-cn.csv" 
[4] "sub-002-Man-lv.csv"
[5] "sub-002-SGU-cn.csv"

Each matrix is asymmetric (i.e., only upper triangle contains values and lower triangle contains zeros). I want to write a loop function to read each file in, conduct a log10 transformation of the upper triangle, and save the output in my working directory with the same name but adding in the suffix "_log.csv". How can I do this in R?

1

There are 1 best solutions below

9
akrun On

We may read the .csv files in to a list with read.csv, transform the data within the list, and then write it back to the same directory after appending _log

files <- list.files(pattern = "\\.csv$", full.names = TRUE)
new_names <- sub(files, "\\.csv$", "_log.csv", files)
lst1 <- lapply(files, function(x) {
              dat <- read.csv(files)
              dat[upper.tri(dat)] <- log10(dat[upper.tri(dat)])
              dat
     })
Map(function(x, y) write.csv(x, file = y, row.names = FALSE, quote = FALSE),
      lst1, new_names)

Or using tidyverse

library(purrr)
library(stringr)
library(readr)
walk(files, ~ {
        dat <- read_csv(.x)
         dat[upper.tri(dat)] <- log10(dat[upper.tri(dat)])
       write_csv(dat, file = str_replace(.x, "\\.csv$", "_log.csv"))
   })